简体   繁体   中英

DBeaver / PostgresSQL fails to connect to remote docker container

I'm trying to access a postgresql docker container through DBeaver, but I can't figure out how to make it work.

Running docker port db_1 returns:

5432/tcp -> 0.0.0.0:5432

So the port should be open to connections.

The postgresql.conf is set to

listen_addresses = '*'

Running docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db_1 returns

172.19.0.2

When trying to connect to the database to either localhost / 127.0.0.1 / 172.19.0.2 / db_1 the Dbeaver log returns this:

2019-03-18 17:22:03.000 - Connect with 'jdbc:postgresql://db_1:5432/test' (postgres-jdbc-169919c23d5-77ac021a71307fee)
....
2019-03-18 17:22:14.815 - SSH INFO: SSH_MSG_SERVICE_ACCEPT received
2019-03-18 17:22:14.824 - SSH INFO: Authentications that can continue: password,keyboard-interactive
2019-03-18 17:22:14.825 - SSH INFO: Next authentication method: password
2019-03-18 17:22:18.432 - SSH INFO: Authentication succeeded (password).
2019-03-18 17:22:18.458 - Connection failed (postgres-jdbc-169919c23d5-77ac021a71307fee)
2019-03-18 17:22:18.459 - SSH INFO: Disconnecting from domain.com port 22
2019-03-18 17:22:18.461 - SSH INFO: Caught an exception, leaving main loop due to Socket closed
2019-03-18 17:22:18.514 - org.jkiss.dbeaver.model.exec.DBCConnectException: The connection attempt failed.
org.jkiss.dbeaver.model.exec.DBCConnectException: The connection attempt failed.
    at org.jkiss.dbeaver.model.impl.jdbc.JDBCDataSource.openConnection(JDBCDataSource.java:179)
    at org.jkiss.dbeaver.ext.postgresql.model.PostgreDataSource.openConnection(PostgreDataSource.java:363)
    at org.jkiss.dbeaver.ext.postgresql.model.PostgreDataSource.initializeRemoteInstance(PostgreDataSource.java:122)
    at org.jkiss.dbeaver.model.impl.jdbc.JDBCDataSource.<init>(JDBCDataSource.java:100)
    at org.jkiss.dbeaver.model.impl.jdbc.JDBCDataSource.<init>(JDBCDataSource.java:89)
    at org.jkiss.dbeaver.ext.postgresql.model.PostgreDataSource.<init>(PostgreDataSource.java:80)
    at org.jkiss.dbeaver.ext.postgresql.PostgreDataSourceProvider.openDataSource(PostgreDataSourceProvider.java:97)
    at org.jkiss.dbeaver.registry.DataSourceDescriptor.connect(DataSourceDescriptor.java:770)
    at org.jkiss.dbeaver.runtime.jobs.ConnectJob.run(ConnectJob.java:70)
    at org.jkiss.dbeaver.ui.dialogs.connection.ConnectionWizard$ConnectionTester.run(ConnectionWizard.java:232)
    at org.jkiss.dbeaver.model.runtime.AbstractJob.run(AbstractJob.java:101)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.Driver$ConnectThread.getResult(Driver.java:405)
    at org.postgresql.Driver.connect(Driver.java:263)
    at org.jkiss.dbeaver.model.impl.jdbc.JDBCDataSource.lambda$0(JDBCDataSource.java:148)
    at org.jkiss.dbeaver.model.impl.jdbc.JDBCDataSource.openConnection(JDBCDataSource.java:157)
    ... 11 more
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:210)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:140)
    at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:109)
    at org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:67)
    at org.postgresql.core.PGStream.receiveChar(PGStream.java:306)
    at org.postgresql.core.v3.ConnectionFactoryImpl.enableSSL(ConnectionFactoryImpl.java:405)
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:94)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
    at org.postgresql.Driver.makeConnection(Driver.java:454)
    at org.postgresql.Driver.access$100(Driver.java:57)
    at org.postgresql.Driver$ConnectThread.run(Driver.java:364)
    at java.lang.Thread.run(Thread.java:748)

Really clueless on how to make the connection work. The SSH tunnel obviously works, but the connection to the db fails. I have a spring boot application running in another container, and connecting that through the connection string jdbc:postgresql://db_1:5432/test works like a charm.

Any input on this? Could it be that 0.0.0.0 doesn't get mapped to the internal network somehow?

Did you get any solution for this ?

I faced the same issue , but my set up was different , i had initially installed Postgres in my local (Postgres1) and had set up a server locally , then i had the docker image (Postgres2) of post-gres . In total i had 2 instances , both were configured to listen to 5432 port (which i realized it later).

Initially i was able to connect to Postgres1 from DBeaver but not the Postgres2, since both were on the same port. My intention was to connect to Postgres2 since my project demanded. so to fix this , i had to goto Services-->postgresql-x64-12--> stop the service, then i was able to connect to Postgres2 and not Postgres1.

Hope this can help.

(Ps: newbie with postgres and docker and my comment may not be clear enough)

How are you exactly running your container, I suspect maybe you are missing to expose the ports? because I just tried running a postgres instance using the following command in PS:

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5433:5432 postgres

Mapping the default postgres container port 5432 to my local 5433 port (because I have a postgres installation locally and didn't want to run into some kind of port-interferance problem) and later on created a database by accessing the docker and running some psql commands as described on this answer and then accesed the DB from DBeaver

在此处输入图片说明

and everything worked fine! Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM