Steps to move location of data folder in Postgres
search cancel

Steps to move location of data folder in Postgres

book

Article ID: 95249

calendar_today

Updated On:

Products

CA Application Performance Management Agent (APM / Wily / Introscope) INTROSCOPE

Issue/Introduction

You want to move the Postgres data folder to another location, perhaps to a new or existing partition that has more free space but without having to reinstall APM. What is the procedure?

The APM installer allows you to select the data directory that Postgres will use. However, administrators usually find out that for a number of reasons (naming policy, backups policies, etc) the folder should had been different so it is required to make Postgres use that other folder.

Environment

These steps apply to all versions of Application Performance Management and Postgres on Linux. A similar procedure exists for Postgres on Windows.

Resolution

Lets assume that the current data folder is under this location:

  /opt/CA/PostgreSQL-9.6.2/data

and you want to move it to this location which is under a different disk with way more free disk space:

   /mnt_BDAPM/

Then, the steps to move location of data folder in Postgres from /opt/CA/PostgreSQL-9.6.2/data to /mnt_BDAPM/data would be

==> As usual, before proceeding make sure you have a valid backup 

1. Make sure the target location exists (/mnt_BDAPM) and is mounted on a disk with enough free disk space
2. First thing to confirm is that we have the correct data directory in Postgres.
   To do so we need to check run a query in Postgres to get the current value of data_directory by running these commands
    a. cd /opt/CA/PostgreSQL-9.6.2/bin
    b. sudo -u postgres ./psql
    c. postgres=# show data_directory;
    d. check the output, it should show something like this:
          data_directory
         ---------------------
        /opt/CA/PostgreSQL-9.6.2/data (1 row)
    e. exit from psql postgres=# \q
3. Stop WebView, MOM and collectors 
4. stop PostgreSQL (note that we use the data path as shown in the previous step so you should use the exact value you get)
    cd /opt/CA/PostgreSQL-9.6.2/bin
    sudo -u postgres ./pg_ctl -D /opt/CA/PostgreSQL-9.6.2/data stop
5. Move data folder to new location
    sudo rsync -av /opt/CA/PostgreSQL-9.6.2/data /mnt_BDAPM
     ==> note that this command "rsync" copies the folder "data" and its subfolders to the new location using the same owner and permissions as the original. The result is that you will have a /mnt_BDAPM/data
6. Check that source (/opt/CA/PostgreSQL-9.6.2/data) and target (/mnt_BDAPM) have the same size
    ==> note that a "data" folder will be created under /mnt_BDAPM and its owner is "postgres"
7. Point Postgres installation to new location. This is done by editing a variable in a script
    a. Edit /opt/CA/PostgreSQL-9.6.2/pg_env.sh
    b. Change value of PGDATA
       from :
         export PGDATA=/opt/CA/PostgreSQL-9.6.2/data
       to:
         export PGDATA=/mnt_BDAPM/data
    c. save changes
8. Start Postgres, this time using the new path
    cd /opt/CA/PostgreSQL-9.6.2/bin
    sudo -u postgres ./pg_ctl -D /mnt_BDAPM/data start
9. Check value of data_directory as in step 2 This time the result should be:
    data_directory
     ----------------- 
    /mnt_BDAPM/data (1 row)
10. Start Collectors, MOM and WebView.

At this point you will have the Postgres' data folder in the new location. If all is working fine you can delete the original folder /opt/CA/PostgreSQL-9.6.2/data

Note1 : ONLY the data folder is moved, all other folders of the Postgres installation "/opt/CA/PostgreSQL-9.6.2/" will remain in the current location so you MUST NOT delete it.

Note2: If Postgres is started automatically you will need to edit the system file /etc/rc.d/init.d/postgresql-9.6.2 (or whatever name it has depending on the version of Postgres) as this file initially has the path to "data" hardcoded and you need to make sure you use the new path to "data".