java - Postgresql docker容器连接失败
问题描述
我正在使用这行代码尝试将(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
码头集装箱 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 上(所以只需在 getConnection args 中使用 5432 并运行我的旧容器将让它起作用)。我可以通过 docker cli 访问数据库,它正在运行。但我要补充一点,我对网络概念很陌生,所以我很容易在这里忽略一些东西。任何帮助深表感谢。
解决方案
5432
我认为您希望将默认 postgres 端口的容器端口发布到 localhost port 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
推荐阅读
- graphql - 序列化和反序列化 GraphQLSchema 到/从数据库
- python - 如何使用 lambda 作为函数参数?
- css - Next.js 字体真棒
- c# - UPDATE QUERY 错误 - 错误路径名不合法
- javascript - 在 Ag-Grid 中使用 valueGetter 换行
- python - 从下拉列表中抓取值列表
- android-studio - 使用 searchview 过滤带有 LiveData 项的 RecyclerView 列表
- scikit-learn - scikit-learn LinearRegression 和 RandomizedSearchCV 及其 n_jobs 参数关系
- java - 如何使用 java 从 .properties 访问环境变量?
- javascript - 在 Javascript 中单击时创建文本和更改文本