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

September 25, 2018 3k views
DigitalOcean Docker PostgreSQL Ubuntu 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
Ajar

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.

Jarland

Have another answer? Share your knowledge.

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