Schemacrawler容器无法连接本地PostgreSQL数据库求助
问题
在Ubuntu 18.04上运行SchemaCrawler Docker容器时,无法连接到本地PostgreSQL数据库,报错连接被拒绝。已修改postgresql.conf设置listen_addresses = '*',并在pg_hba.conf添加host all all 0.0.0.0/0 md5,但问题依旧。
运行的命令:
docker run \ --mount type=bind,source="$(pwd)",target=/home/schcrwlr \ --rm -it \ schemacrawler/schemacrawler:v16.19.7 \ /opt/schemacrawler/bin/schemacrawler.sh \ --server postgresql \ --host localhost \ --user postgres \ --password pass \ --database detections \ --info-level standard \ --command schema \ --output-file output.png
错误信息:
Error: Could not connect to <jdbc:postgresql://localhost:5432/detections>, for <user 'postgres'>, with properties <{}> java.lang.RuntimeException: Could not connect to <jdbc:postgresql://localhost:5432/detections>, for <user 'postgres'>, with properties <{}> at us.fatehi.utility.datasource.AbstractDatabaseConnectionSource.getConnection(AbstractDatabaseConnectionSource.java:131) at us.fatehi.utility.datasource.SimpleDatabaseConnectionSource.get(SimpleDatabaseConnectionSource.java:112) at us.fatehi.utility.datasource.SimpleDatabaseConnectionSource.get(SimpleDatabaseConnectionSource.java:49) at schemacrawler.tools.commandline.command.ConnectCommand.loadSchemaRetrievalOptionsBuilder(ConnectCommand.java:180) at schemacrawler.tools.commandline.command.ConnectCommand.run(ConnectCommand.java:124) at schemacrawler.tools.commandline.SchemaCrawlerCommandLine.executeCommandLine(SchemaCrawlerCommandLine.java:110) at schemacrawler.tools.commandline.SchemaCrawlerCommandLine.execute(SchemaCrawlerCommandLine.java:72) at schemacrawler.Main.main(Main.java:76) Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:319) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:247) at org.postgresql.Driver.makeConnection(Driver.java:434) at org.postgresql.Driver.connect(Driver.java:291) at us.fatehi.utility.datasource.AbstractDatabaseConnectionSource.getConnection(AbstractDatabaseConnectionSource.java:122) ... 7 more Caused by: java.net.ConnectException: Connection refused (Connection refused) at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:412) at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255) at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237) at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.base/java.net.Socket.connect(Socket.java:609) at org.postgresql.core.PGStream.createSocket(PGStream.java:241) at org.postgresql.core.PGStream.<init>(PGStream.java:98) at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:109) at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235)
解决方案
问题核心是Docker容器的网络隔离:容器内的localhost指向容器自身,而非宿主机的PostgreSQL服务。以下是两种可行的解决方法:
方法1:使用宿主机的Docker内部IP
查找宿主机在Docker网络中的网关IP:
ip addr show docker0输出中
inet字段对应的IP(通常为172.17.0.1)就是容器可访问的宿主机地址。修改SchemaCrawler命令的
--host参数为该IP:docker run \ --mount type=bind,source="$(pwd)",target=/home/schcrwlr \ --rm -it \ schemacrawler/schemacrawler:v16.19.7 \ /opt/schemacrawler/bin/schemacrawler.sh \ --server postgresql \ --host 172.17.0.1 \ # 替换为实际查到的Docker网关IP --user postgres \ --password pass \ --database detections \ --info-level standard \ --command schema \ --output-file output.png
方法2:使用Docker host网络模式
运行容器时添加--network host参数,让容器直接使用宿主机的网络栈,此时容器内的localhost等同于宿主机的localhost:
docker run \ --mount type=bind,source="$(pwd)",target=/home/schcrwlr \ --rm -it \ --network host \ schemacrawler/schemacrawler:v16.19.7 \ /opt/schemacrawler/bin/schemacrawler.sh \ --server postgresql \ --host localhost \ --user postgres \ --password pass \ --database detections \ --info-level standard \ --command schema \ --output-file output.png
额外验证步骤
- 重启PostgreSQL服务,确保配置修改生效:
sudo systemctl restart postgresql - 在宿主机本地执行
psql -h localhost -U postgres detections,验证数据库服务本身可正常连接。
内容的提问来源于stack exchange,提问作者laos
相关产品推荐
相关产品推荐

