1$ mkdir postgres2$ cd ./postgres
1$ cd ./postgres2$ mkdir ./pgdata
👉 As a docker container is "stateless" we must create the directory "pgdata" to keep the data when the container is shut down.
1$ touch ./docker-compose.yml
1version: "3.8"2services:3 db:4 image: "postgres:13"5 ports:6 - "5432:5432"7 volumes:8 - ./pgdata:/var/lib/postgresql/data9 environment:10 - POSTGRES_USER=dbuser11 - POSTGRES_PASSWORD=admin202112 - POSTGRES_DB=todoapp
This file creates a host called "db" from a Postgres version 13 image. The TCP port 5432 (postgres) of the host "db" is exposed externally as TCP port 5432.
The local directory "./pgdata" is mapped as "/var/lib/postprogressql/data" inside the "db" host
The username, password and database name are exposed as an environment variable
1$ docker-compose up -d
1$ docker-compose ps
1$ docker-compose run db bash
1$ psql --host=db --username=dbuser --dbname=todoapp
1$ psql --host=localhost --username=dbuser --dbname=todoapp
1$ docker-compose down
Et voilà 🎉 !
We deliver high quality blog posts written by professionals monthly. And we promise no spam.