Categories
docker

Postgres on Docker

Don’t care about why you would want to run Postgres in Docker? Skip to the commands.

Applications and Websites are a little bit useless without data.

So we intrepid developers like to hold that data somewhere to be used later. There are many forms of storage that we can use, such as cookies, local storage in the browser, a SQLite database for our iOS and Android apps or a database that we can pull data out of using an API.

When working on a project that uses a database, it can be painful to keep every ones environments in sync – if Alice is using Postgres version 4 and Bob is using version 6 there could be a time where Bob writes some procedure that runs fine on his database but when Alice tries to run it – it blows up!

This is where Docker comes in handy, you can ship a Dockerfile in your projects source code, put it up on Github and tell all the developers working on your project to run that Dockerfile and you can rest assured that your code will continue to work.

In this tutorial I am going to explain how to install & use Postgres, a popular relational database.

Lets get started!

Installing Docker

First we need to setup Docker if you haven’t already done so. Once that is done, make sure Docker is running by opening your terminal and typing.

docker --version 

If you get an error along the lines of;

Cannot connect to the Docker daemon. Is the docker daemon running on this host?

You need to run Docker CE. How to do that changes between operating systems click here for a thread on how to troubleshoot that.

If everything went well you should see the current version number and are ready to go on to the next steps.

Creating a persistent volume

We want to create a folder on our computer so when we restart the Docker container all of our data will still be there. If we didn’t take this step, each time we start and stop our container we would be starting from scratch!

Open your terminal and type the following to create that volume.

mkdir -p $HOME/docker/volumes/postgres

Pulling Postgres Image from DockerHub

Here we are going to see the beauty of Docker. So the Postgres team have already created a preset image that developers who want to use Postgres can download and use. It contains all the code packaged in a way that can be shared and used with confidence by all the developers in your team. Okay, lets hop back over to your terminal and run;

docker pull postgres

Running a container using the Postgres Image

Now we want to create an instance of a database (i.e we want to create a container using the Postgres image we just pulled as a base) and we need to pass it some parameters for it to work the way we want it to.

docker run \
    --rm --name pg-docker \
    -e POSTGRES_PASSWORD=docker \
    -d -p 5432:5432 \
    -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data \
    postgres

So lets unpack this a little;

  • docker run -rm –name pg-docker
    • Run an container (removing it if it already exists) called pg-docker.
  • -e POSTGRES_PASSWORD=docker
    • Set the environment variable POSTGRES_PASSWORD to ‘docker’
  • -d -p 5432:5432
    • -d = Run in detached mode (i.e return control to the terminal after running.)
    • -p Map local port 5432 to Container port 5432 (the default Postgres Port)
  • -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data
    • Map the local volume we created earlier to the internal folder on the container named /var/lib/postgresql/data
  • postgres
    • Use the image named ‘postgres’ which we pulled earlier

Congrats!

You should now be running Postgres on Docker!

You can verify by running

docker ps | grep pg-docker

Which should show you the following;

Postgres running in Docker!

Now you can use psql to connect to the running instance, or connect to it from your applications, or view the database using a GUI tool like DBeaver!

Fancy making something useful now you’ve got Postgres set up? Click here to see how to use Postgres to make a location-based social network.


TL;DR

docker pull postgres;
mkdir -p $HOME/docker/volumes/postgres;
docker run \
    --rm --name pg-docker \
    -e POSTGRES_PASSWORD=docker \
    -d -p 5432:5432 \
    -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data \
    postgres
psql -h localhost -U postgres -d postgres