Better Programming

Advice for programmers.

Follow publication

Connect From Your Local Machine to a PostgreSQL Database in Docker

A simple How To to get you up and running with Docker

Lorenz Vanthillo
Better Programming
Published in
3 min readMar 19, 2018

Docker makes it very easy to spin up a PostgreSQL database management system. With the following command it is possible to start your PostgreSQL Docker container on your server or local machine:

$ docker run -d -p 5432:5432 --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword postgres

This command will start a PostgreSQL database and map ports using the following pattern: -p <host_port>:<container_port>.
Port 5432 of our container will be mapped on port 5432 of our host or server.

Access the container on your host or server. We will create a database inside our PostgreSQL container.

$ docker exec -it my-postgres bash

Now you are ‘inside’ your container. We can access postgres and create the database.

root@cb9222b1f718:/# psql -U postgres
psql (10.3 (Debian 10.3-1.pgdg90+1))
Type "help" for help.
postgres=# CREATE DATABASE mytestdb;
CREATE DATABASE
postgres=#\q

We are finished. You can exit your container (\q) and go to your local machine. Here you need some PostgreSQL Client tool installed:

  • PSQL (CLI)
  • PgAdmin

My PostgreSQL container is running on my local machine, which explains why I am connecting with localhost. If it is running on a specific server, use your server IP. (For Windows docker-machine you probably need to use 192.168.99.100).

$ psql -h localhost -p 5432 -U postgres -W                       Password for user postgres:                       
psql (9.5.5, server 10.3 (Debian 10.3-1.pgdg90+1)) WARNING: psql major version 9.5, server major version 10. Some psql features might not work.
Type "help" for help. postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype |
-----------+----------+----------+------------+------------+
mytestdb | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
| | | | |
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
| | | | |
(4 rows)

After authenticating you will see the mytestdb is in the list of available databases. Now you can connect with your database using \c .

For a tool like PgAdmin you can define your connection. Also, here you have to replace localhost with your server IP if your container is running elsewhere.

Create a server connection
Define your connection

Save the connection and you can connect to the database which is running in your PostgreSQL Docker container!

This post was based on a popular SO answer I gave. I hope it helps!

If it really helped you… :)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Lorenz Vanthillo
Lorenz Vanthillo

Written by Lorenz Vanthillo

AWS Community Builder | DevOps | Docker Certified Associate | 5x AWS Certified | CKA Certified | https://lvthillo.com

Responses (8)

Write a response