Postgresql Changing Data Directory in Ubuntu

Moving Postgres 10 data dir on Ubuntu 18

after a clean ubuntu installation (clean all partiotions), auto mounting my second drive and using the answer from here everything works!

  1. installed postgresql-common via:

    sudo apt install postgresql-common

  2. open createcluster.conf with nano

    sudo nano /etc/postgresql-common/createcluster.conf

  3. uncomment line create_main_cluster = true

  4. change line data_directory = '/var/lib/postgresql/%v/%c' to new/dir/%v/%c

  5. save file

  6. install postgresql via sudo apt install -y postgresql-10

How can I move postgresql data to another directory on Ubuntu over Amazon EC2?

Stop the server.

Copy the datadir while retaining permissions - use cp -aRv.

Then (easiest, as it avoids the need to modify initscripts) just move the old datadir aside and symlink the old path to the new location.

How to move location of postrgresql 13 database

Assuming your configuration files are located under $PG_DATA, where they belong:



  1. Shut down the (old) database
  2. Copy the data directory to the new location (use cp -rp, or rsync -acv, or tar, or cpio, ...) Make sure that file attributes and ownership are preserved by the copy. The pgdata directory should be mode == 0600, and owner.group == postgres.postgres.
  3. [optionally] rename the old data directory
  4. [optionally] you may want to edit the configuration files at the new location
  5. edit the startup file (in /etc/init.d/postgresql ) and make sure $PG_DATA points to the new location. [note: this is for ubuntu; other distributions may us a different starting mechanism]
  6. Start the new database, and check if it runs (ps auxw| grep postgres, and if you can connect (psql -U postgres postgres)
  7. [optionally] remove the directory tree at the old location.

Moving postgres data folder on Ubuntu

  • You can just copy or move the directory, including all subdirs and files
    cp -rp or mv should be enough for this.
  • Postgres must not be running while you are messing with the files
  • The base of the data-drectory (PG_DATA) must be owned by postgres and have file mode 0700 . (when not: pg will refuse to start)
  • [the rest of the files must at least be readable/writeble by postgres]
  • the new location must also be known to the startup process (in /etc/init.d/ and (possibly) in the postgres.conf file within the data directory. (for the log file location)


Related Topics



Leave a reply



Submit