Introduction
This article is focusing on how to run PostgreSQL database server. Actually, it is a continuation from the previous article which exist in ‘How to Run PostgreSQL Database Server using a Docker Container in Microsoft Windows. But there is a slight modification in this article to the docker container running PostgreSQL database server. That modification is changing the PostgreSQL database server’s PGDATA folder to be available in the host or the local device. The reason for having that folder is to be able to persist the data instead of having it in a docker image. In that case, it is still possible for accessing the PostgreSQL database server’s data without having to run the docker container.
How to Run PostgreSQL Database using a Docker Container and Mount the PGDATA to a Folder in a Host
Following after in this part, just perform the simulation for attaching the PGDATA of a PostgreSQL database server of a docker container. Below are the steps for achieving it :
-
First of all, just make sure that the docker container is not active. In order to do that, just stop the container. But first of all, check the available docker container before :
Microsoft Windows [Version 10.0.22000.1455] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 36639c3f7d8a postgres "docker-entrypoint.s…" 4 hours ago Up 3 hours 5432/tcp db C:\Users\Personal>
-
Apparently, the PostgreSQL database server container is currently running using a simple docker-compose command. It is a docker-compose for running using the docker-compose.yml configuration file. In this case, the PostgreSQL database server is already running with its PGDATA folder. Below is a command for stopping the PostgreSQL database server docker container :
C:\Users\Personal>docker container stop 83943 83943 C:\Users\Personal>
-
Soon after that, just check the status of that PostgreSQL database server docker container. It is just to make sure that it is already stop and it is not running. Below is the command execution for that purpose :
C:\Users\Personal>docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8394377a23ab postgres:14.2 "docker-entrypoint.s…" 2 hours ago Exited (0) 1 second ago db C:\Users\Personal>
-
Last but not least, as the last step, just mount the folder into the local folder exist in the host. Just type the following command :
C:\database\postgresql>docker run --name postgres-0 -e POSTGRES_PASSWORD=password -e PGDATA=/var/lib/postgresql/data/pgdata -v C:\database\postgresql\pgdata:/var/lib/postgresql/data/pgdata -d -p 6000:5432 postgres 36639c3f7d8a940f9368986f18cddb027aa34375220c30d647b7543f91ebb004 C:\database\postgresql>
-
Finally, just for a confirmation, go to the pgdata folder. Check whether all of the files of PostgreSQL database server exist as follows :
C:\database\postgresql>cd pgdata C:\database\postgresql\pgdata>dir Volume in drive C is Windows-SSD Volume Serial Number is CA30-19A4 Directory of C:\database\postgresql\pgdata 01/21/2023 12:47 PM <DIR> . 01/21/2023 12:46 PM <DIR> .. 01/21/2023 12:47 PM <DIR> base 01/21/2023 12:47 PM <DIR> global 01/21/2023 12:47 PM <DIR> pg_commit_ts 01/21/2023 12:47 PM <DIR> pg_dynshmem 01/21/2023 12:47 PM 4,821 pg_hba.conf 01/21/2023 12:47 PM 1,636 pg_ident.conf 01/21/2023 12:47 PM <DIR> pg_logical 01/21/2023 12:47 PM <DIR> pg_multixact 01/21/2023 12:47 PM <DIR> pg_notify 01/21/2023 12:47 PM <DIR> pg_replslot 01/21/2023 12:47 PM <DIR> pg_serial 01/21/2023 12:47 PM <DIR> pg_snapshots 01/21/2023 12:47 PM <DIR> pg_stat 01/21/2023 12:47 PM <DIR> pg_stat_tmp 01/21/2023 12:47 PM <DIR> pg_subtrans 01/21/2023 12:47 PM <DIR> pg_tblspc 01/21/2023 12:47 PM <DIR> pg_twophase 01/21/2023 12:47 PM 3 PG_VERSION 01/21/2023 12:47 PM <DIR> pg_wal 01/21/2023 12:47 PM <DIR> pg_xact 01/21/2023 12:47 PM 88 postgresql.auto.conf 01/21/2023 12:47 PM 29,525 postgresql.conf 01/21/2023 12:47 PM 112 postmaster.opts 01/21/2023 12:47 PM 101 postmaster.pid 7 File(s) 36,286 bytes 19 Dir(s) 7,394,488,320 bytes free C:\database\postgresql\pgdata>cd .. C:\database\postgresql>
Fortunately, it exist and so the process is a success.