Run Postgres in Docker with One Command

May 15, 2020

Prerequites

You have installed Docker.

For Mac OS X, you can follow here to install Docker for Mac.

THE Command

$ docker run -d --name my_postgres -v my_dbdata:/var/lib/postgresql/data -p 5432:5432 postgres:11.2-alpine

The command comes from this site.

Explanation

-d is running the container in detached mode, so you can continue your shell session to run other commands.

--name my_postgres is giving the container a name.

-v my_dbdata:/var/lib/postgresql/data is using a Docker volume. You can view it as letting the docker container access a host directory.

You can check the volume info with command:

$ docker volume ls                                                                                                                  
DRIVER              VOLUME NAME
local               my_dbdata

$ docker volume inspect my_dbdata                                                                                                   
[
    {
        "CreatedAt": "2020-05-15T11:42:46Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/my_dbdata/_data",
        "Name": "my_dbdata",
        "Options": null,
        "Scope": "local"
    }
]

The -p 5432:5432 part means it is mapping the host port 5432 to the container 5432, which is the default port Postgres listens to. With this, you can’t access the running Postgres via host port 5432.

If you have a local copy postgres running on port 5432, you can map to a different host port, say 15432, you can change the part like this: -p 15432:5432.

Finally, the command runs a Docker container based on postgres:11.2-alpine, a small image running Alpine Linux. The base image is only 5MB.

How to restart docker container?

If you restart your computer and try the same command again, you would get this error:

$ docker run -d --name my_postgres -v my_dbdata:/var/lib/postgresql/data -p 5432:5432 postgres:11.2-alpine
docker: Error response from daemon: Conflict. The container name "/my_postgres" is already in use by container "2f9b0e4139fda805e49a8588c495a4d317e0a35866e642dce61c43cecd1b6346". You have to remove (or rename) that container to be able to reuse that name.

This is because the container has already been created. You can check docker:

$ docker ps -a                                                                        
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS                    PORTS                                    NAMES
2f9b0e4139fd        postgres:11.2-alpine   "docker-entrypoint.s…"   5 days ago          Exited (255) 3 days ago   0.0.0.0:5432->5432/tcp                   my_postgres

To restart it, you can do

$ docker restart my_postgres

How to access the database

You can use any database client to access the database. e.g., with psql:

$ psql -U postgres --port 5432 -h localhost

The default username and password are both postgres.

Why run PostgreSQL in Docker? Why not install directly?

Because it’s hard to install directly. There could be other dependencies that fail your installation and you don’t know how to fix.

Running inside Docker container doesn’t have this issue, because it is an isolated environment. When you docker pull postgres-alpine, you pull the entire running environment which is setup properly for you.

Also, because Docker is isolated from your local machine. You don’t have to worry about messing with your other dependencies.

Finally, you can run multiple versions of the Postgres easily with Docker. It is a matter of pulling the image with the right version tag.

Closing thoughts

Hopefully you now know how simple it is to run Postgres in docker with one command.

If you want to learn more, sign up for my SQL course at BackToSQL.com.

Thank you for your read!




 Share: