1

I need to save a database to an external HDD. This question has been asked previously here. However, upon changing the data_directory variable in the postgresql.conf file that exists in /usr/local/var/postgres/ to

data_directory = /path/to/externalHDD/directory/

I then restart the PostgreSQL server with brew services restart postgresql, but when I try to reconnect to the database via

psql -U username -d postgres

I receive the following error:

psql: error: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

How do I fix this?

psa
  • 25
  • 5
  • Too little information. Please tell us *all* the steps you took. How did you move the data directory, etc. – Laurenz Albe Apr 28 '21 at 06:53
  • Did you start the database server? And does it run? Please check the log files. – Frank Heikens Apr 28 '21 at 06:54
  • @LaurenzAlbe I changed the variable specifying the data directory `data_directory` to `data_directory = /path/to/externalHDD/directory/` in the `postgresql.conf` file that exists in `/usr/local/var/postgres/`. I'm on MacOS and I used Homebrew to install, so I then restarted the PostgreSQL server with the command `brew services restart postgresql`. I then ran `psql -U username -d postgres` and the error followed. Is there anything else I should expand on specifically? – psa Apr 28 '21 at 07:58
  • @FrankHeikens Yes, I restarted it using the `brew` command stated above. How do I find the log files? – psa Apr 28 '21 at 08:00
  • I meant to add the additional information to the query... – Laurenz Albe Apr 28 '21 at 08:01

1 Answers1

2

If all you did was point data_directory somewhere else, that cannot work, because there is no data directory at the destination. You have to create the data directory using initdb.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • Sorry, I'm almost brand new to PostgreSQL. How would one create the data directory using `initdb`? From [this](https://www.postgresql.org/docs/9.5/app-initdb.html) documentation it seems that the `directory` argument specified in `initdb [option...] [--pgdata | -D] directory` should be the desired directory on the external HDD, and then point `data_directory` to this same directory? – psa Apr 28 '21 at 08:10
  • Yes, pretty much. Then `postgresql.conf` will be in the original location, and the data directory on your external disk. But you might as well use the new data directory as argument to `pg_ctl start -D` (if that's how you start it) and use the `postgresql.conf` there. If that's all confusing, I understand. You should perhaps read some PostgreSQL documentation and familiarize yourself with `initdb` and `pg_ctl`. – Laurenz Albe Apr 28 '21 at 10:10