How to Use PGDATA PostgreSQL Database Server Folder from Linux for PostgreSQL Database Server in Microsoft Windows

Posted on

Introduction

This is an article where the main focus is for using a different PGDATA or a different data folder for running a PostgreSQL database server. In this article, the original PostgreSQL database server is running in a local device using Microsoft Windows operating system.  So, the main purpose is for using another data folder for that PostgreSQL database server. That data folder exist from the running docker container. Actually, the running docker container is running a PostgreSQL database image where the data folder is mounted to the local device. It exist as in ‘How to Run PostgreSQL Database using a Docker Container and Mount the PGDATA to a Folder in a Host‘. But that one is just for a temporary testing. In this article, the purpose is to make it permanent.

How to Use PGDATA PostgreSQL Database Server Folder from Linux for PostgreSQL Database Server in Microsoft Windows

This is the part for achieving the goal permanently change the PGDATA PostgreSQL database server. As for the sequence, below are the steps to do it :

  1. First of all, just edit the registry of PostgreSQL database server. So, in the first step, just run ‘regedit’ as follows :

    How to Use PGDATA PostgreSQL Database Server Folder from Linux for PostgreSQL Database Server in Microsoft Windows
    How to Use PGDATA PostgreSQL Database Server Folder from Linux for PostgreSQL Database Server in Microsoft Windows
  2. As for the entry in the regedit tool, just make sure to access HKEY_LOCAL_MACHINE > System > CurrentControlSet > Services > postgresql_service. In this example, the postgresql_service is postgresql-x64-14. As for the example in this article, it uses PostgreSQL 14.2 running in a local device using Microsoft Windows 11. Furthermore, the entry for changing the configuration of PGDATA PostgreSQL database server folder location is in ‘ImagePath’. By Default, the value depends on the environment of the host, but in this example it is as follows :

    "C:\Program Files\PostgreSQL\14\bin\pg_ctl.exe" runservice -N "postgresql-x64-9.5" -D "C:\Program Files\PostgreSQL\14\data" -w
    How to Use PGDATA PostgreSQL Database Server Folder from Linux for PostgreSQL Database Server in Microsoft Windows
    How to Use PGDATA PostgreSQL Database Server Folder from Linux for PostgreSQL Database Server in Microsoft Windows

    Before changing the entry of the ‘ImagePath’, just make sure that it can run by executing it using a command line. For an example of the execution it exist in ‘How to Connect to PostgreSQL Database Server from Microsoft Windows using a PostgreSQL PGDATA from Linux Docker Container‘. For the case of this article, the command execution will be as follows :

    pg_ctl.exe -D "C:\Program Files\PostgreSQL\14\data"
  3. Right click at the ItemPath and choose Modify as in the following image appearance :

    How to Use PGDATA PostgreSQL Database Server Folder from Linux for PostgreSQL Database Server in Microsoft Windows
    How to Use PGDATA PostgreSQL Database Server Folder from Linux for PostgreSQL Database Server in Microsoft Windows
  4. Following after, just change the value of the ImagePath to the path of the other PGDATA. As an example in this article, it exist in ‘C:\database\postgresql\14.2\pgdata’. So, the value in this context will be :

    "C:\Program Files\PostgreSQL\14\bin\pg_ctl.exe" runservice -N "postgresql-x64-9.5" -D "C:\database\postgresql\14.2\pgdata" -w
  5. Soon after, just access Services from the host as in the following appearance :

    How to Use PGDATA PostgreSQL Database Server Folder from Linux for PostgreSQL Database Server in Microsoft Windows
    How to Use PGDATA PostgreSQL Database Server Folder from Linux for PostgreSQL Database Server in Microsoft Windows
  6. Last but not least, just check and make sure that the PGDATA is already changed in the properties of the services. In order to do that, just right click at the service name and choose Properties. Look at the Path to executable part of the Properties. It should have the same value as in the registry editor of the PostgreSQL database server. Below is the appearance of the PostgreSQL database service’s property :

    How to Use PGDATA PostgreSQL Database Server Folder from Linux for PostgreSQL Database Server in Microsoft Windows
    How to Use PGDATA PostgreSQL Database Server Folder from Linux for PostgreSQL Database Server in Microsoft Windows

    Just click start button in the properties to start. If it is a success and it does not generate any specific error, it will run properly as it shows above.

Leave a Reply