postgresql docker容器连接失败

djmepvbi  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(497)

我使用这行代码尝试连接(w/jdbc)到psql docker容器:

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

db name、username和pw都是postgres。容器是用

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

此处出错:

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容器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

这同一行代码将连接到一个不同的psql容器,我刚才做的,我能看到他们之间的唯一区别是,另一个是在端口5432(所以只要使用5432在getconnection参数和运行我的旧容器将使它工作)。我可以通过docker cli访问db,它正在运行。但我要补充的是,我对网络概念非常陌生,所以我很容易忽略了这里的一些东西。非常感谢您的帮助。

vc9ivgsu

vc9ivgsu1#

我想你想要集装箱港口 5432 这是要发布到localhost端口的默认postgres端口 5431 . 而不是

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

使用

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

相关问题