1

I hava a PostgreSQL database on my PC. The Postgres server is running on port 5432. When I go on another PC and type in telnet server.ip 5432, I manage to connect. However when I try to connect using Java:

connection = DriverManager.getConnection("Jdbc:postgresql:mydb://server.ip:5432/", "user", "pass");

I get the following error: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. Can someone tell me what the problem might be?

UPDATE:

I changed the connection string to:

connection = DriverManager.getConnection("jdbc:postgresql://server.ip:5432/mydb", "user", "pass");

and now I am getting the error FATAL: no pg_hba.conf entry for host "client.ip", user "user", database "mydb", SSL off

2 Answers 2

2

Your JDBC connection string is wrong. The documentation says these are the allowed formats:

jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database

but not - as in your case:

Jdbc:postgresql:databse://host:port/

(also note the capital letter J in your string which is also not allowed)

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

Comments

1

You need to config auth file /etc/postgresql/x.x/main/pg_hba.conf

Add these lines:

# TYPE DATABASE  USER  ADDRESS                   METHOD
host   all       all   my.pc.ip/Prefix-netmask   md5

After that restart the postgresql server:

# /etc/init.d/postgresql restart

2 Comments

Hmmm. The question says, my.pc.ip is the IP of the server. But the server's config file must contain the IP of the remote client.
I added the remote client IP in pg_hba.conf and it worked. Thanks @A.H.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.