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.