Bootstrap FreeKB - Postgres (SQL) - Install Postgres on Docker
Postgres (SQL) - Install Postgres on Docker

Updated:   |  Postgres (SQL) articles

A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a Postgres server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage a memory (containers are usually megabytes in size).

 

The docker pull command can be used to pull down the latest Postgres image.

~]# sudo docker pull postgres
Using default tag: latest
latest: Pulling from library/postgres
f03b40093957: Pull complete
9d674c93414d: Pull complete
de781e8e259a: Pull complete
5ea6efaf51f6: Pull complete
b078d5f4ac82: Pull complete
97f84fb2a918: Pull complete
5a6bf2f43fb8: Pull complete
f1a40e88fea4: Pull complete
4be673794a1a: Pull complete
9d72f84fb861: Pull complete
5d52569da92e: Pull complete
5d48fbe991ff: Pull complete
4ae692d11ad3: Pull complete
Digest: sha256:31c9342603866f29206a06b77c8fed48b3c3f70d710a4be4e8216b134f92d0df
Status: Downloaded newer image for postgres:latest
docker.io/library/postgres:latest

 

The docker images command can be used to display the Postgres image.

~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
postgres     latest    0c88fbae765e   2 weeks ago   379MB

 

The following command can then be used to create and start the Posgres container. Let's break down this command.

  • The docker run command is used to create and start the container.
  • The --detach flag is used to run the container in the background.
  • The --publish option is used to configure both the Docker server and container to listen on port 5432, which adds a rule to iptables to allow connections between the Docker system and container on port 5432.
  • The --env option is used to define the MARIADB_ROOT_PASSWORD variable.
  • The --name option is used to name the container postgres.
  • The --restart unless-stopped option is used so that the container is started if the Docker server is restarted
  • The mariadb image is used.
docker run
--detach
--publish 5432:5432
--env POSTGRES_PASSWORD=itsasecret
--name postgres
--restart unless-stopped
postgres

 

The docker container ls command can be used to ensure the container is running.

~]# docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS                                       NAMES
c132e43cd93a   postgres "docker-entrypoint.s..."  About a minute ago   Up About a minute   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   postgres

 

The docker logs command should return something like this.

PostgreSQL init process complete; ready for start up.
2023-06-09 20:05:34.401 UTC [1] LOG:  starting PostgreSQL 15.3 (Debian 15.3-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2023-06-09 20:05:34.402 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-06-09 20:05:34.402 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-06-09 20:05:34.402 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-06-09 20:05:34.404 UTC [61] LOG:  database system was shut down at 2023-06-09 20:05:34 UTC
2023-06-09 20:05:34.427 UTC [1] LOG:  database system is ready to accept connections

 

The psql --list command should return the following.

~]$ sudo docker exec postgres psql -U postgres --list
                                                List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    | ICU Locale | Locale Provider |   Access privileges
-----------+----------+----------+------------+------------+------------+-----------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |            | libc            |
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 |            | libc            | =c/postgres          +
           |          |          |            |            |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 |            | libc            | =c/postgres          +
           |          |          |            |            |            |                 | postgres=CTc/postgres
(3 rows)

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter d0b63e in the box below so that we can be sure you are a human.