Introduction
This is a continuation from the previous article. That is an article exist in ‘How to Run PostgreSQL Database Server using a Docker Container in Microsoft Windows. That article is showing about how to use a local folder exist in the host of the docker server to mount the PGDATA of PostgreSQL database server which is previously running using docker container. After that, in order to have another PGDATA which is possible for further access from a running docker container, this article just focus on showing how to connect to a PostgreSQL database server. Since there is a physical folder of PGDATA exist in the folder of the host, there is chance to use it further. In this case, use the PGDATA folder for the PostgreSQL database server which is running in the host of the docker container in the local device.
How to Connect to PostgreSQL Database Server from Microsoft Windows using a PostgreSQL PGDATA from Linux Docker Container
Actually, it is very simple for achieving this purpose. But as a test at first, it will just use the PGDATA without having the PostgreSQL database server’s docker container to have run. So, in order to do that, make sure that the PostgreSQL database server’s docker container is stop or it is not running. Below is the sequences of the step in order to be able to accomplish it :
-
First of all, this just check the PostgreSQL database server’s docker container by typing the following command :
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>
-
Actually the docker container is running, just turn it off or stop it by executing the following command :
C:\Users\Personal>docker container stop 83943 83943 C:\Users\Personal>
-
And then following after, just check the status of the docker container by typing the command as follows :
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>
-
So, according to the above output, the PostgreSQL database server’s docker container is not running anymore. In other words, the PGDATA folder of the PostgreSQL database server running in the docker container is currently not in use. It is the perfect time to test it as a PGDATA for running PostgreSQL database server. In this context, it is for the one running in the host or the local device. Before start to use the PGDATA of the PostgreSQL database server, check the PGDATA folder first. Actually, it exist in a location with the path of ‘C:\database\postgres’. Below is the appearance of the folder :
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>
-
Finally, after knowing the exact location of the PGDATA of PostgreSQL database server, just execute the following command. It is a command for running PostgreSQL database service in the host or the local device as exist below :
C:\Program Files\PostgreSQL\14\bin>pg_ctl.exe -D "C:\database\postgreSQL\pgdata" start waiting for server to start....2023-01-21 08:26:21.232 UTC [38356] LOG: starting PostgreSQL 14.2, compiled by Visual C++ build 1914, 64-bit 2023-01-21 08:26:21.234 UTC [38356] LOG: listening on IPv6 address "::", port 5432 2023-01-21 08:26:21.234 UTC [38356] LOG: listening on IPv4 address "0.0.0.0", port 5432 2023-01-21 08:26:21.280 UTC [36976] LOG: database system was shut down at 2023-01-21 08:13:39 UTC 2023-01-21 08:26:21.306 UTC [38356] LOG: database system is ready to accept connections done server started C:\Program Files\PostgreSQL\14\bin>
Last but not last, , just access the PostgreSQL database server. It is a step for proving that the PostgreSQL database server is currently running. As for the term ‘running’, it is running using the PGDATA folder of the PostgreSQL database server from the docker container. Just do it as follows :
C:\Program Files\PostgreSQL\14\bin>psql -Upostgres psql (14.2) WARNING: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly. See psql reference page "Notes for Windows users" for details. Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows) postgres=# \q
In the above output, the process for accessing the PostgreSQL database server is a success. In other words, the process is a success.