To change the data directory location for PostgreSQL after installation

 To change the data directory location for PostgreSQL after installation, you'll need to move the existing data to the new location and update PostgreSQL's configuration to use the new directory. Here’s a step-by-step guide to achieve that:

1. Stop PostgreSQL service:

First, stop the PostgreSQL service to ensure no data is being accessed or written during the move.


sudo systemctl stop postgresql-15

2. Create the new data directory:

Create the new directory where you want to move your PostgreSQL data, and set the appropriate permissions for the postgres user.


sudo mkdir -p /new/postgres/pg_data sudo chown postgres:postgres /new/postgres/pg_data sudo chmod 700 /new/postgres/pg_data

3. Move the existing data:

Move your existing data from the old directory to the new directory. This step will ensure all your current databases and configurations are preserved.


sudo rsync -av --progress /var/lib/pgsql/15/data/ /new/postgres/pg_data/
  • The rsync command helps preserve permissions and ownership during the move.
  • Make sure the postgres user owns the new directory and its contents.

4. Update the PostgreSQL configuration:

Once the data has been copied, you need to inform PostgreSQL about the new data directory by updating the service configuration.

Option 1: Update the PGDATA Environment Variable:

Edit the PostgreSQL service configuration to point to the new data directory:


sudo vi /usr/lib/systemd/system/postgresql-15.service

Look for the following line (or similar):


Environment=PGDATA=/var/lib/pgsql/15/data/

Change it to:


Environment=PGDATA=/new/postgres/pg_data

Save the file and reload the systemd configuration:


sudo systemctl daemon-reload

Option 2: Change in PostgreSQL configuration file (if no PGDATA variable is used):

You can also modify the PostgreSQL configuration file (postgresql.conf) to change the data_directory parameter. Open the configuration file (the original one) and update the data_directory entry.


sudo vi /new/postgres/pg_data/postgresql.conf

Look for the line:

#data_directory = '/var/lib/pgsql/15/data'

Update it to:

data_directory = '/new/postgres/pg_data'

Save and exit.

5. Start PostgreSQL with the new data directory:

Now, start the PostgreSQL service with the updated configuration:

sudo systemctl start postgresql-15

6. Verify that PostgreSQL is using the new data directory:

Check the status of PostgreSQL to ensure it's running, and verify that it's using the new data directory:


sudo systemctl status postgresql-15

You can also connect to PostgreSQL and check the data directory location:


sudo -u postgres psql -c 'SHOW data_directory;'

This should return the new data directory path.

7. Clean up the old data directory (optional):

If everything is working correctly and you've verified the new data directory is in use, you can remove the old data directory:


sudo rm -rf /var/lib/pgsql/15/data

That's it! PostgreSQL should now be using the new data directory. Let me know if you need any further help.

Comments

Popular posts from this blog

AWR

GIT