0

My goal is to store my postgresql database data on an external ssd hard drive that I have plugged in to a machine running Linux Ubuntu 20.04 LTS ARM x64. I am running into problems when changing the data directory in the postgresql.conf file and then restarting postgres.

psql: could not connect to server: No such file or directory

Is the server running locally and accepting connections on 
Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

I am aware this error occurs when there is a problem with postgres configuration. I have tried setting permissions and ownership for user postgres on the /mnt/data/postgresql/12/main directory. No luck.

Below are the steps I took:

Create mount directory

sudo mkdir /mnt/data

Mount drive to directory

sudo mount -o defaults /dev/sda1 /mnt/data

Add directory to mounted drive where I want to store postgres data

sudo mkdir -p /mnt/data/postgresql/12/main

Change the data directory to /mnt/data/postgresql/12/main

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

Restart postgresql service

sudo systemctl restart postgresql.service

1 Answer 1

2

You should use initdb to create a new data directory as user postgres, e.g.

sudo mkdir -p /mnt/data/postgresql/12/main
sudo chown -R postgres:postgres /mnt/data/postgresql
sudo -u postgres /usr/lib/postgresql/12/bin/initdb -D /mnt/data/postgresql/12/main
sudo service postgresql stop

This also creates (unused) config files postgresql.conf, pg_hba.conf and pg_ident.conf in /mnt/data/postgresql/12/main.

Now change the path to data_directory in /etc/postgresql/12/main/postgresql.conf, start the service and test if the data_directory is set as expected:

sudo service postgresql start
sudo -u postgres psql -c 'show data_directory;'

This should output:

        data_directory        
------------------------------
 /mnt/data/postgresql/12/main
(1 row)
6
  • thank you for this. When I run the initdb command I am getting an error saying: fixing permissions on existing directory /mnt/data/postgresql/12/main ... initdb: error: could not change permissions of directory "/mnt/data/postgresql/12/main": Operation not permitted. I made sure I used sudo but I am assuming since we switched to postgres with -u that the postgres user doesn't have permission to do this? Commented Feb 10, 2021 at 18:00
  • Run sudo chown -R postgres:postgres /mnt/data/postgresql before "initdb", the directory needs to belong to user postgres.
    – Freddy
    Commented Feb 10, 2021 at 18:07
  • That got me through the initdb which looks like I have a bunch of new config files in the /mnt/data/postgresql/12/main folder. I had to do sudo su postgres to be able to CD into it. Can you explain what this did exactly? Commented Feb 10, 2021 at 18:24
  • You don't need to edit the config files in /mnt/data/postgresql/12/main, just change data_directory in /etc/postgresql/12/main/postgresql.conf and start the service. The default password is not set, use sudo to switch to the user. Please read postgresql.org/docs/12/app-initdb.html.
    – Freddy
    Commented Feb 10, 2021 at 18:30
  • 1
    This is the clean way for a new database instance and I think it's the fastest solution to switch the data directory (edit one line in the original config, easy to revert).
    – Freddy
    Commented Feb 10, 2021 at 18:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .