PostgreSQL on Docker

Run PostgreSQL as a Docker container : fast/quick play approach

Run a PostgreSQL container from the latest tag and with the default configuration and no volume mounted  (no volume means no data persisted after container removal!) :
docker run --name my-postgres -d postgres

Execute interactive commands on that container with the psql command :
docker exec -ti my-postgres psql -U postgres
Here is the psql official documentation for the version 12.

Run PostgreSQL as a Docker container : specific version, custom postgre configuration and data persistence outside the container

Set the PostgreSQL server configuration of the container

First step : retrieve and store on the host the sample PostgreSQL conf of the image :
docker run -i postgres:15.2 cat /usr/share/postgresql/postgresql.conf.sample > postgresql.conf.sample

Second step : modify the configuration file and run the image with that configuration enabled (bind mount + use the postgres cmd with the configuration file as property) :
docker run -d --name my-postgres -v "$PWD/my-postgresql.conf":/etc/postgresql/postgresql.conf postgres -c 'config_file=/etc/postgresql/postgresql.conf'

Another way to set the database configuration is setting values in the docker command of the started container :
Example :
docker run -d --name flask-postgres -e POSTGRES_DB=flask_db -e POSTGRES_PASSWORD=secret postgres:15.2 -c 'shared_buffers=256MB' -c 'max_connections=200'
Note: POSTGRES_DB is optional, it allows to specify the name of the database to create.

About database, user and password

The default postgres user and database are created in the entrypoint (initdb).
The postgres database is a default database meant for use by users, utilities and third party applications.
That’s why the only mandatory env variable is : POSTGRES_PASSWORD.
While optiona, if we pass the env envariable POSTGRES_USER, its value is used to create the user and the db associated to. Otherwise the default postgres user will be used.

Run the PostgreSQL container by specifying the source of the data volume

Run a PostgreSQL container from a specific tag and with my-postgres-data, the source volume mounted for postgresql data :
docker run --name my-postgres -v my-postgres-data:/var/lib/postgresql/data -d postgres:12.1

Declare a service PostgreSQL in docker-compose

Here we define a specific PostgreSQL version,  a named volume for data (to be able to reuse it outside the current container),  the password, the user (that will be also the db name).
Fix : the password has to be hidden.

  database:
    image: postgres:12.1
    volumes:
      - my-postgres-data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: pass
      POSTGRES_USER: david

Run PostgreSQL as a Docker container managed by the linux services system on the host

Maybe when I would need that…

Ce contenu a été publié dans Non classé. Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *