Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to a Postgresql set up on docker, running on local host, with dbeaver?

I am running a postgresql database on docker, hosting it on my local machine. When I try and connect to it using DBeaver though, I am given the error:

Connection to localhost:5432 refused.
Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Connection refused: connect

Here are the commands I ran in Docker to create my PostgreSQL image:

rrowe@LAPTOP-AFM43BOB MINGW64 /c/Program Files/Docker Toolbox
$ docker run --name RiohRoweDB -e POSTGRES_PASSWORD=Charlie -d -p 5432:5432 postgres
55c78d059fdb2b19bfdd24f579eaf26ef5b00c77ead564ee7d128fd06996789d

rrowe@LAPTOP-AFM43BOB MINGW64 /c/Program Files/Docker Toolbox
$ docker exec -it RiohRoweDB bash
root@55c78d059fdb:/# psql -U postgres
psql (12.0 (Debian 12.0-2.pgdg100+1))
Type "help" for help.

postgres=# CREATE DATABASE postgresqlDB
postgres-# \q
root@55c78d059fdb:/# exit
exit

rrowe@LAPTOP-AFM43BOB MINGW64 /c/Program Files/Docker Toolbox
$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
55c78d059fdb        postgres            "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        0.0.0.0:5432->5432/tcp   RiohRoweDB

On the DBeaver side, I add a connection with the following parameters:

Host:localhost
Port:5432
Database:postgresqlDB
User:postgres
Password:Charlie

When I test the connection ("Test Connection" button in Connection Settings)

I get the following error:

Connection to localhost:5432 refused.
Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Connection refused: connect

I was following this tutorial that did not use DBeaver: https://medium.com/better-programming/connect-from-local-machine-to-postgresql-docker-container-f785f00461a7

like image 808
Rioh Rowe Avatar asked Oct 31 '19 03:10

Rioh Rowe


Video Answer


1 Answers

If you got here from the official Postgres Docker page, don't forget to add the port-forwarding flag -p 5432:5432 to your initialization command like Rioh did above:

docker run --name RiohRoweDB -e POSTGRES_PASSWORD=Charlie -d -p 5432:5432 postgres

Also consider studying this how-to on Medium:

https://medium.com/@wkrzywiec/database-in-a-docker-container-how-to-start-and-whats-it-about-5e3ceea77e50

like image 85
David Maddox Avatar answered Sep 17 '22 14:09

David Maddox