1

I'm using this line of code to try to connect(w/ jdbc) to a psql docker container:

Connection conn = DriverManager.getConnection(
   "jdbc:postgresql://localhost:5431/postgres?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC",
               "postgres", "postgres");

db name, username, and pw are all postgres. The container was created with

docker run --name practice -e POSTGRES_PASSWORD=postgres -p 5431:5431 postgres

Error here:

org.postgresql.util.PSQLException: The connection attempt failed.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:150)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
        at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
        at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
        at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
        at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:30)
        at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
        at org.postgresql.Driver.makeConnection(Driver.java:393)
        at org.postgresql.Driver.connect(Driver.java:267)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
        at com.dehmer.JdbcSelectTest.main(JdbcSelectTest.java:9)
Caused by: java.io.EOFException
        at org.postgresql.core.PGStream.ReceiveChar(PGStream.java:276)
        at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:269)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:108)
        ... 11 more

docker container ls:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
be8931d1621c        postgres            "docker-entrypoint.s…"   About an hour ago   Up About an hour    0.0.0.0:5431->5431/tcp, 5432/tcp   practice

This same line of code will connect to a different psql container I made a while ago, the only difference I can see between them is that the other one is on port 5432 (so just using 5432 in the getConnection args and running my old container will make it work). I can access the db through the docker cli just fine and it is running. But I'll add that I'm very new to networking concepts, so I could easily be overlooking something here. Any help is much appreciated.

1 Answer 1

4

I think you want the container port 5432 which is the default postgres port to be published to localhost port 5431. Instead of

docker run --name practice -e POSTGRES_PASSWORD=postgres -p 5431:5431 postgres

Use

docker run --name practice -e POSTGRES_PASSWORD=postgres -p 5431:5432 postgres
Sign up to request clarification or add additional context in comments.

Comments

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.