Question

How can I setup a docker postgres container on DigitalOcean and connect to it?

I’ve created a droplet with docker pre-installed I’ve pulled the official Postgres image and used the following command to run it

docker run --name postgres -e POSTGRES_PASSWORD=****** -e POSTGRES_USER=**** -d postgres

This image auto exposes the default Postgres port 5432 I can see the container running using: docker ps but cannot connect to it I’m using jetbrains datagrip sql client to connect. I’ve filled the ip host, user/password I’ve set on the Postgres container. My droplet inclues my ssh keys and I could verify that I’m able to connect to my droplet but not to my container. I assume I should use such a tcp client to connect to my droplet ip on 5432 port and I’m suppose to be able to connect to it, right? Did I miss anything? I’ve also tried setting up a network driver using:

docker network create --driver bridge postgres-network

and after stoping and removing the container to recreate it like so:

docker run --name db-postgres --network postgres-network -e POSTGRES_USER=**** POSTGRES_PASSWORD=****** -d postgres

It didn’t work either… Can you please assist me? Thank you Ajar


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

jarland
DigitalOcean Employee
DigitalOcean Employee badge
September 25, 2018
Accepted Answer

Hey friend!

For that first part, run this instead:

docker run -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=****** -e POSTGRES_USER=**** -d postgres

The container may expose the port, but this will bind it to a port outside of the container for accessibility.

Jarland

This is an old post but since it has no answer i’ll leave one here.

The problem you have is that the port 5432 in which your postgres DB is running is an internal port inside the container, the only thing you’re missing is to map the internal port of your container (5432) to a port inside your localhost so you can connect to it.

Solution: Just add a “-p {any-localhost-port}:5432” to your command and you’ll be good to go.

Example:

docker run --name postgres -e POSTGRES_PASSWORD=****** -e POSTGRES_USER=**** -p 5000:5432 -d postgres

Hope this is helpful for someone in the future. Enjoy your coding journey!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel