0

I am developing an Android application and I'm using PostgreSQL bundled with PostGIS. I want to use a local database for that but connecting via 'localhost' doesn't work, I have tried the alternative to use '127.0.0.1' instead but it is not working, I am getting the following error:

org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

Using my network's ipv4 adress works, but I cannot connect via localhost. How can I fix that? My pg_hba.conf looks like:

...
# Put your actual configuration here
# ----------------------------------
#
# If you want to allow non-local connections, you need to add more
# "host" records.  In that case you will also need to make PostgreSQL
# listen on a non-local interface via the listen_addresses
# configuration parameter, or via the -i or -h command line switches.



# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all             all             192.168.1.1/24          trust
host    all             all             0.0.0.0/0               trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

# Allow replication connections from localhost, by a user with the
# replication privilege.
#host    replication     postgres        127.0.0.1/32            trust
#host    replication     postgres        ::1/128                 trust

and my postgresql.conf looks like:

# - Connection Settings -

listen_addresses = '*'      # what IP address(es) to listen on;
                    # comma-separated list of addresses;
                    # defaults to 'localhost'; use '*' for all
                    # (change requires restart)
port = 5123         # (change requires restart)
max_connections = 100           # (change requires restart)

My code in Eclipse looks like

private String DB_URL = "jdbc:postgresql://" + Server.getDbserver() + ":" + Server.getDbport() + "/postgis";

And my variables inside my Server class look like.

private static String dbserver = "localhost";
private static int dbport = 5123;

As a summary, connecting via my network ipv4 (for example 192.168.1.199) works, but connecting via 'localhost' or '127.0.0.1' generates me the error I posted above.

8
  • So, PostgreSQL is actually running inside the Android OS? Commented Dec 2, 2014 at 9:57
  • I am not sure I understand what you want to say. Commented Dec 2, 2014 at 10:19
  • This is a repost of dba.stackexchange.com/questions/83851. It's no small feat to install a postgresql server on Android, see Porting Postgresql server to Android and it can't be done while being not sure about what you're doing. Commented Dec 2, 2014 at 10:22
  • 2
    Presumably the postgresql server is running on some host OS running an Android emulator or another machine on your LAN, so it's normal that Android can't reach it through localhost but can through a LAN address. Commented Dec 2, 2014 at 10:23
  • 1
    @AlexandruMitu Well, in that case it isn't "localhost", as the Android emulator is a virtual machine. It's effectively a different computer. So PostgreSQL isn't acessible on the Android OS's 127.0.0.1 loopback address ("localhost"), only via the virtual network between the simulated Android device and your PC. Commented Dec 2, 2014 at 11:10

2 Answers 2

1

Why are you using 192.168.1.1/24 and trust? Try it maybe like this:

host all all 192.168.1.1/32 md5

And then implement the connection with username / password.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your observation, robin. But that doesn't help my cause.
0

Well, you need a connection between your Android device and your computer but unfortunately, the USB cable does not do that. However, if you do want to rule your application offline, you will have to make your PC or your Android device a hotspot. I would not reccomend you to make your tablet a hotspot, but making a computer a hotspot is pretty easy, you can use Connectify and voila, your laptop is connected to the same LAN as your computer and you will get the computer ipv4 provided to you by Connectify itself. Have fun.

1 Comment

Thank you for this answer, this is what I was looking for, I will try what you said.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.