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

Posted September 25, 2018 8.7k views
DigitalOceanDockerPostgreSQLUbuntu 18.04

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

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.

Submit an Answer
1 answer

Hey friend!

For that first part, run this instead:

docker run -p 5432:5432 –name postgres -e POSTGRESPASSWORD=****** -e POSTGRESUSER=**** -d postgres

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