Stop installing postgres on your laptop : use Docker instead

Tired of managing a full-blown DB server on your development machine ?

Let’s give Docker a try !

Getting started with Docker

First, if your development machine is not running Linux (ie if you’re running windows or mac os x), it’s easier to use boot2docker to get started, since it will take care of installing:

  • VirtualBox (if you already have it installed, don’t worry, it won’t overwrite your current configuration)
  • A Linux VM (based on the latest boot2docker.iso, that will run virtusalized in Virtual Box)
  • Boot2Docker Management Tool v1.4.1
  • Docker Client v1.4.1

Once you’re able to successfully run :

you’re ready to proceed with PostgreSQL

PostgreSQL running on Docker for development

Let’s grab the latest version of the PostgreSQL image from Docker official repository, and let’s start the database :

A few explanation about this command line :

  • -p 5432:5432 : means that we port forward the container (effectively running the db) 5432 port to the host (in my case, running boot2docker, the VirtualBox vm named boot2docker-vm) 5432 port – since the postgres image EXPOSE the port 5432 for its service, we will be able to connect to the db using this port
  • –name : the name of your container – you can see all your containers and their names using the

    command
  • -e : sets an environment variable (in our case the postgres user password will be “password”)
  • -d : runs the container in detached mode
  • postgres : the name of the image (by default the latest version)

Let’s have a look at the newly created container :

We just got confirmation that our container, named postgres, is running the latest postgres image, and does the proper port forwarding.

Oh ! one last thing ! If using boot2docker, you have to know the ip address of the container’s host :

That’s it ! You’re all set ! You just installed a PostgreSQL db on your laptop without polluting your environment (it’s dockerized !), and you can access it from your app connecting to 192.168.59.103 on port 5432 as postgres with the password “password”

Interesting things to know

Client tools

I used to run PostgreSQL on a mac, using homebrew, and along with the server came the client tools such as pgql, pg_restore, pg_dump.

Now I use the ones bundled with pgAdmin3, on a typical mac install they are located in /Applications/pgAdmin3.app/Contents/SharedSupport :

Stopping / starting your dockerized PostgreSQL server

When you start working with your DB, you may want to not loose your changes. Hopefully, Docker can save the changes brought to your container.

If your container is running, find its id :

Then stop it :

or since the container is named :

To restart it, it’s just as easy as :

You can check it’s back on with :

Another approach (more production oriented approach) would be to use data volume containers

Boot2docker shutting down

From times to times (when my laptop goes to sleep or connect to another wifi hotspot) my boot2docker virtual box vm shuts itself down : so I restart it with