Sunday, November 22, 2026
TR
Linux

How to Move the PostgreSQL Data Directory to a New Location on Ubuntu 22.04

How to Move the PostgreSQL Data Directory to a New Location on Ubuntu 22.04

Before you begin moving PostgreSQL's data directory, let's verify the current location by starting an interactive PostgreSQL session. In the command below, psql is the command that enters the interactive monitor, and it makes the command run as the system's postgres user: -u postgres tells sudo to run psql

sudo -u postgres psql

Once the PostgreSQL prompt is open, use the following command to show the current data directory:

SHOW data_directory;
Output       data_directory
-----------------------------
 /var/lib/postgresql/14/main
(1 row)

This output confirms that PostgreSQL is configured to use the default data directory, /var/lib/postgresql/14/main, which is the directory you need to move. Once you have confirmed the directory on your system, you can close the psqlprompt by running the meta command: \q

\q

To ensure the integrity of the data, stop PostgreSQL before actually making any changes to the data directory:

sudo systemctl stop postgresql

systemctl does not display the result of all service management commands. To verify that you have stopped the service successfully, use the following command:

sudo systemctl status postgresql

The output should tell you that PostgreSQL is inactive (dead), meaning it has been stopped:

Output○ postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor>
     Active: inactive (dead) since Thu 2022-06-30 18:46:35 UTC; 27s ago
    Process: 4588 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 4588 (code=exited, status=0/SUCCESS)
        CPU: 1ms

Now that the PostgreSQL server is no longer running, copy the existing database directory to the new location with rsync. Using the -a flag preserves permissions and other directory properties, while -v provides verbose output so you can follow the progress. To mimic the original directory structure in the new location, you will run rsync from the postgresql directory. By creating this directory inside the mount point directory postgresql and retaining ownership by the PostgreSQL user, you can avoid permission problems for future upgrades.

Note: TAB Make sure there is no trailing slash on the directory, which may be added if you use completion. If you add a trailing slash, rsync will dump the contents of the directory into the mount point instead of copying the directory itself.

The version directory, 14 is not strictly necessary since you explicitly define the location in the postgresql.conf file, but following the project convention certainly does not hurt, especially if running multiple PostgreSQL versions is needed in the future:

sudo rsync -av /var/lib/postgresql /mnt/volume_nyc1_01

After the copy is complete, rename the current folder with a .bak extension and keep it until you confirm that the move was successful. This will help avoid confusion that could result from having similarly named directories in both the new and old locations:

sudo mv /var/lib/postgresql/14/main /var/lib/postgresql/14/main.bak

You are now ready to configure PostgreSQL to access the data directory in its new location.

Step 2 — Pointing to the New Data Location

By default, the data_directory configuration directive is set to /var/lib/postgresql/14/main in the /etc/postgresql/14/main/postgresql.conf file. Edit this file to reflect the new data directory:

sudo nano /etc/postgresql/14/main/postgresql.conf

Find the line that begins with data_directory and change the path that follows it to reflect the new location. In the context of this tutorial, the updated directive will be written as follows: /etc/postgresql/14/main/postgresql.conf

. . .
data_directory = '/mnt/volume_nyc1_01/postgresql/14/main'
. . .

Save and close the file by pressing CTRL + X, Y and then ENTER. That is all you need to do to configure PostgreSQL to use the new data directory location. At this point, all that is left is to restart the PostgreSQL service and check that it actually points to the correct data directory.

Step 3 — Restarting PostgreSQL

After changing the data-directory directive in the postgresql.conf file, go ahead and start the PostgreSQL server using systemctl:

sudo systemctl start postgresql

To verify that the PostgreSQL server started successfully, check its status again using systemctl:

sudo systemctl status postgresql

If the service started correctly, the Active line in the command output will say: active (exited)

Output● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor>
     Active: active (exited) since Thu 2022-06-30 18:50:18 UTC; 3s ago
    Process: 4852 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 4852 (code=exited, status=0/SUCCESS)
        CPU: 1ms

Finally, to make sure the new data directory is actually in use, open the PostgreSQL prompt:

sudo -u postgres psql

Check the value for the data directory again:

SHOW data_directory;
Output             data_directory
----------------------------------------
 /mnt/volume_nyc1_01/postgresql/14/main
(1 row)

This confirms that PostgreSQL is using the new data directory location. After this, take a moment to make sure you can access your database and interact with the data in it. Once you have verified the integrity of the existing data, you can remove the backup data directory:

sudo rm -Rf /var/lib/postgresql/14/main.bak

With that, you have successfully moved your PostgreSQL data directory to a new location.

Kerim Koç

Systems and infrastructure engineer. I write about Linux, open source, high availability and security.