Percona Database Performance Blog (mysqlperf) wrote,
Percona Database Performance Blog
mysqlperf

Installing PostgreSQL using Docker

https://www.percona.com/blog/installing-postgresql-using-docker/

https://www.percona.com/blog/?p=67630

install postgresql with docker

Following the series of blogs started by Peter Zaitsev in Installing MySQL with Docker, on deploying Docker containers running open source databases, in this article, I’ll demonstrate how to install PostgreSQL using Docker.

Before proceeding, it is important to remind you of Peter’s warning from his article, which applies here as well: 

“The following instructions are designed to get a test instance running quickly and easily; you do not want to use these for production deployments.” 

We intend to show how to bring up PostgreSQL instance(s) into Docker containers. Moreover, you will find some basic Docker management operations and some snippets containing examples, always based on the Docker Hub PostgreSQL official image.

Installing

Starting with the installation, the following example shows how to bring up a Docker container within the latest PostgreSQL release from Docker Hub. It is required to provide the password of the Postgres user at the moment you are launching the container; otherwise, the container will not be created:

$ docker run --name dockerPostgreSQL \
-e POSTGRES_PASSWORD=secret \
-d postgres:latest

Once you start the container, you can check if it successfully started with docker inspect:

$ docker inspect -f '{{.State.Running}}' dockerPostgreSQL
true

If not, you can try starting it manually (you will see how to do it on the Managing the PostgreSQL Containers section).

Getting Connected to the Containerized Instance

The snippet below will show you how to connect to your PostgreSQL containerized instance.

  • Directly from docker:

$ docker exec -it dockerPostgreSQL psql --user postgres
postgres=# \conninfo

You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".

  • Or from a PostgreSQL client connecting to its IP address, which you can obtain with the command docker inspect:

$ docker inspect -f '{{.NetworkSettings.IPAddress}}' dockerPostgreSQL
172.16.0.12

$ psql --user postgres --port 5432 --host 172.16.0.12
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "172.16.0.12" at port "50432".

Managing the PostgreSQL Containers

The command below will show you how to list all active containers and their respective statuses:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
7f88656c4864        postgres:11         "docker-entrypoint..."   4 minutes ago       Up 4 minutes        0.0.0.0:50432->5432/tcp   dockerPostgreSQL11
8aba8609dabc        postgres:11.5       "docker-entrypoint..."   21 minutes ago      Up 21 minutes       5432/tcp                  dockerPostgreSQL115
15b9e0b789dd        postgres:latest     "docker-entrypoint..."   32 minutes ago      Up 32 minutes       5432/tcp                  dockerPostgreSQL

To stop one of the containers, do:

$ docker stop dockerPostgreSQL

To start a container:

$ docker start dockerPostgreSQL

Finally, to remove a container, first stop it and then:

$ docker rm dockerPostgreSQL

Customizing PostgreSQL settings in the container

Let’s say you want to define different parameters for the PostgreSQL server running on Docker. You can create a custom postgresql.conf configuration file to replace the default one used with Docker:

$ docker run --name dockerPostgreSQL11 -p 50433:5432 -v "$PWD/my-postgres.conf":/etc/postgresql/postgresql.conf -e POSTGRES_PASSWORD=scret -d postgres:11

Another example of how to pass startup arguments is changing the directory which stores the wal files:

$ docker run --name dockerPostgreSQL11 -p 50433:5432 -v "$PWD/my-postgres.conf":/etc/postgresql/postgresql.conf -e POSTGRES_PASSWORD=scret -e POSTGRES_INITDB_WALDIR=/backup/wal -d postgres:11

Running Different PostgreSQL Versions in Different Containers

You can launch multiple PostgreSQL containers, each running a different PostgreSQL version. In the example below, you will find how to start up a 10.5 PostgreSQL version:

$ docker run --name dockerPostgreSQL105 -p 50433:5432 -e POSTGRES_PASSWORD=scret  -d postgres:10.5
7f51187d32f339688c2f450ecfda6b7552e21a93c52f365e75d36238f5905017

Here is how you could launch a PostgreSQL 11 container:

$  docker run --name dockerPostgreSQL11 -p 50432:5432 -e POSTGRES_PASSWORD=scret -d postgres:11
7f88656c4864864953a5491705ac7ca882882df618623b1e5664eabefb662733

After that, by reissuing the status command, you will see all of the already created ones:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
e06e637ae090        postgres:10.5       "docker-entrypoint..."   4 seconds ago       Up 4 seconds        0.0.0.0:50433->5432/tcp   dockerPostgreSQL105
7f88656c4864        postgres:11         "docker-entrypoint..."   About an hour ago   Up About an hour    0.0.0.0:50432->5432/tcp   dockerPostgreSQL11
15b9e0b789dd        postgres:latest     "docker-entrypoint..."   2 hours ago         Up 2 hours          5432/tcp                  dockerPostgreSQL

You will notice that both 10.5 and 11 were created with different port mappings. Locally into the container, both are listening on 5432 but are externally mapped to different ports. Therefore, you will be able to access all of them externally.

You will find even more details on the PostgreSQL Hub Docker site.

Percona Distribution for PostgreSQL provides the best and most critical enterprise components from the open-source community in a single distribution, designed and tested to work together.

Download Percona Distribution for PostgreSQL Today!

Subscribe
  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments