How to run Postgres in Docker

If you need to run Postgres database for development needs you can just install it manually on local machine. But you should be aware that this procedure will require some level of knowledge about Postgres installation and maintaining procedures.

On the other hand there is much more simple way - run Postgres database inside Docker container. Docker effectively incapsulates deployment, administration and configuration procedures. So if you want to deploy Postgres locally with minimum efforts Docker is the best choice. All you need to do is just start pre-build Docker container and you will have Postgres database ready for your service.

Here is my github repo to build Docker container with embedded Postgres database:

(If you don't have Docker yet, please read how to install it in my previous article here.)

To build Docker container you will need to run 3 simple commands in terminal:

git clone  
docker build -t postgres dockerized-postgres  
docker run -di -p 5432:5432 postgres  

Here you are cloning project from github, building container and launching it.

So now you can verify Postgres database instance running on your local machine:

telnet 5432  
Connected to  
Escape character is '^]'.  

Possibly you can wonder why we use ip address and not if we run it locally? This is actually default address of virtual machine running Docker container which hosts Postgres database for us. If your container has different ip address, please consider to use it instead of default one.

To authorise into Postgres database following default credentials should be used: postgres/postgres.

How is it working? (for those who actually care)

Repository you just clonned contains 3 files:

  • Dockerfile
  • pg_hba.conf

This is main file which instructs Docker to create new container based on Ubuntu image, download Postgres distributive, configure and proceed to script:

FROM ubuntu:14.04

RUN apt-get update -y  
RUN apt-get install postgresql postgresql-contrib -y

RUN mv /etc/postgresql/9.3/main/pg_hba.conf /etc/postgresql/9.3/main/pg_hba.conf.backup  
COPY pg_hba.conf /etc/postgresql/9.3/main/pg_hba.conf  
RUN echo "listen_addresses = '*'" >> /etc/postgresql/9.3/main/postgresql.conf


COPY /  

Here we start Postgres database and setting default login and password for access:

sudo service postgresql start  
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'postgres'"  

The last tail command is needed to assure that script never completes and it effectively makes container to run in background, otherwise it will stop execution immediately after startup.

local   all             postgres                                peer  
host    all             all               md5  
host    all             all                  md5

This file is exclusively used to configure Postgres to accept connections from remote hosts. By default Postgres permits external connections only from localhost, so when running in Docker you cant access it from your host machine. Thus we add pg_hba.conf configuration file to permit all inbound connections from any machines.


So now you have got Postgres database running locally without deep knowledge about it's internals. What is also useful to know that there are pre-build Docker containers for almost all kind of applications you can imagine like email server or Hadoop cluster. And so you can effectively rely on them to start complex applications in minutes and save time by avoiding manual installation and configuration.