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

How To Move a PostgreSQL Data Directory to a New Location on Ubuntu 22.04
Not using Ubuntu 22.04?Choose a different version or distribution.
Ubuntu 22.04


Databases grow over time, sometimes outgrowing the space on their original file system. When they’re located on the same partition as the rest of the operating system, this can potentially lead to I/O contention.

RAID, network block storage, and other devices can offer redundancy and improve scalability, along with other desirable features. Whether you’re adding more space, evaluating ways to optimize performance, or looking to take advantage of other storage features, this tutorial will guide you through relocating PostgreSQL’s data directory.


To complete this guide, you will need:

The examples throughout this tutorial will involve moving the data to a block storage device mounted at /mnt/volume_nyc1_01. If you’re using Block Storage on DigitalOcean, read our documentation on How to Create and Set Up Volumes for Use with Droplets to guide you on mounting your volume before continuing with this tutorial.

Regardless of what underlying storage you use, though, the following steps can help you move the data directory to a new location.

Step 1 — Moving the PostgreSQL Data Directory

Before we get started with moving PostgreSQL’s data directory, let’s verify the current location by starting an interactive PostgreSQL session. In the following command, psql is the command to enter the interactive monitor and -u postgres tells sudo to execute psql as the system’s postgres user:

  1. sudo -u postgres psql

Once you have the PostgreSQL prompt opened up, use the following command to show the current data directory:

  1. SHOW data_directory;
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, so that’s the directory you need to move. Once you’ve confirmed the directory on your system, you can close the psql prompt by running the \q meta-command:

  1. \q

To ensure the integrity of the data, stop PostgreSQL before you actually make changes to the data directory:

  1. sudo systemctl stop postgresql

systemctl doesn’t display the outcome of all service management commands. To verify that you’ve successfully stopped the service, use the following command:

  1. sudo systemctl status postgresql

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

○ 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 the permissions and other directory properties, while -v provides verbose output to help you follow the progress. You’re going to start the rsync from the postgresql directory in order to mimic the original directory structure in the new location. By creating that postgresql directory within the mount-point directory and retaining ownership by the PostgreSQL user, you can avoid permissions problems for future upgrades.

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

The version directory, 14, isn’t strictly necessary since you’ve defined the location explicitly in the postgresql.conf file, but following the project convention certainly won’t hurt, especially if there’s a need in the future to run multiple versions of PostgreSQL:

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

Once the copy is complete, rename the current folder with a .bak extension and keep it until you’ve confirmed that the move was successful. This will help to avoid confusion that could arise from having similarly-named directories in both the new and the old location:

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

Now you’re 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:

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

Find the line that begins with data_directory and change the path which follows to reflect the new location. In the context of this tutorial, the updated directive will be written as:

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

Save and close the file by pressing CTRL + X, Y, then ENTER. This is all you need to do to configure PostgreSQL to use the new data directory location. All that’s left at this point is to start the PostgreSQL service again and check that it is indeed pointing 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:

  1. sudo systemctl start postgresql

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

  1. sudo systemctl status postgresql

If the service started correctly, the Active line will say active (exited) in the command’s 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

Lastly, to make sure that the new data directory is indeed in use, open the PostgreSQL command prompt:

  1. sudo -u postgres psql

Check the value for the data directory again:

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

This confirms that PostgreSQL is using the new data directory location. Following this, take a moment to ensure that you’re able to access your database as well as interact with the data within. Once you’ve verified the integrity of any existing data, you can remove the backup data directory:

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

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


If you’ve followed along, your database should be running with its data directory in the new location and you’ve completed an important step toward being able to scale your storage. You might also want to check out 5 Common Server Setups For Your Web Application for ideas on how to create a server infrastructure to help you scale and optimize web applications.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us

About the authors

Default avatar

Manager, Developer Education

Technical Writer @ DigitalOcean

Default avatar

Technical Writer

Educator and writer committed to empowering our community by providing access to the knowledge and tools for making creative ideas into a reality

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I follow these steps except in part 2 I use /home/postgresql for my new data location - I have most of my available space allocated to my home directory and want the postgresql database there.

Outside of this I follow all of your steps and when I restart the service it starts for systemctl start postgresql and when I run status.

But, when i try sudo - u postgres psql I get this error

SpongeBob@SpongeBoB:/var/lib# sudo -u postgres psql psql: error: connection to server on socket “/var/run/postgresql/.s.PGSQL.5432” failed: No such file or directory Is the server running locally and accepting connections on that socket?

Any idea why this is? Do I have to use /mnt?

This comment has been deleted

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Featured on Community

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel