Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres not working when I change the data directory in Ubuntu

I am new in Linux and Postgres. I have been trying to connect my data to another directory for 2 days without success.

First I changed in my postgres.conf the data directory from:

data_directory = '/var/lib/postgresql/9.6/main'

To:

data_directory = '/media/cesar/My Book/data9.6'

When I try to connect to Postgres I get this error:

cesar@ubuntu:/$ sudo -u postgres psql 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 restarted the Postgres Server several times and changed the configuration also without success.

If I change back to the old directory it works fine.

cesar@ubuntu:/$ sudo /etc/init.d/postgresql restart
[sudo] password for cesar: 
[ ok ] Restarting postgresql (via systemctl): postgresql.service.
cesar@ubuntu:/$ sudo -u postgres psql postgres
psql (9.6.2)
Type "help" for help.
postgres=#

Any idea of what I am doing wrong?

ls -s response

postgres@ubuntu:~$ ls -l /media/cesar/My\ Book/data9.6
total 149
drwxrwxrwx 1 cesar cesar     0 Jan  7 22:04 base
drwxrwxrwx 1 cesar cesar 16384 Feb  6 19:40 global
drwxrwxrwx 1 cesar cesar     0 Jan  7 21:56 pg_clog
drwxrwxrwx 1 cesar cesar     0 Jan  7 21:56 pg_commit_ts
drwxrwxrwx 1 cesar cesar     0 Jan  7 21:56 pg_dynshmem
-rwxrwxrwx 2 cesar cesar  4118 Jan  8 14:46 pg_hba.conf
-rwxrwxrwx 2 cesar cesar  1678 Jan  7 21:56 pg_ident.conf
drwxrwxrwx 1 cesar cesar 65536 Feb  6 00:16 pg_log
drwxrwxrwx 1 cesar cesar     0 Jan  7 21:56 pg_logical
drwxrwxrwx 1 cesar cesar     0 Jan  7 21:56 pg_multixact
drwxrwxrwx 1 cesar cesar     0 Feb  6 00:16 pg_notify
drwxrwxrwx 1 cesar cesar     0 Jan  7 21:56 pg_replslot
drwxrwxrwx 1 cesar cesar     0 Jan  7 21:56 pg_serial
drwxrwxrwx 1 cesar cesar     0 Jan  7 21:56 pg_snapshots
drwxrwxrwx 1 cesar cesar     0 Feb  6 00:16 pg_stat
drwxrwxrwx 1 cesar cesar     0 Feb  7 21:15 pg_stat_tmp
drwxrwxrwx 1 cesar cesar     0 Jan  7 21:56 pg_subtrans
drwxrwxrwx 1 cesar cesar     0 Jan  7 21:56 pg_tblspc
drwxrwxrwx 1 cesar cesar     0 Jan  7 21:56 pg_twophase
-rwxrwxrwx 2 cesar cesar     4 Jan  7 21:56 PG_VERSION
drwxrwxrwx 1 cesar cesar 32768 Feb  6 00:15 pg_xlog
-rwxrwxrwx 2 cesar cesar    90 Jan  7 21:56 postgresql.auto.conf
-rwxrwxrwx 1 cesar cesar 22267 Feb 12 14:25 postgresql.conf
-rwxrwxrwx 2 cesar cesar    67 Feb  6 00:16 postmaster.opts
-rwxrwxrwx 2 cesar cesar    35 Feb  6 00:16 postmaster.pid
-rwxrwxrwx 1 cesar cesar     3 Feb 12 14:52 test.txt
like image 587
CesarD Avatar asked Feb 12 '17 18:02

CesarD


People also ask

How move Postgres data directory Ubuntu?

Changing the Data folder Location on Postgresql Configuration Files. We can change the default data folder by editing the /etc/postgresql/9.5/main/postgresql. conf file and edit the data_directory. $ sudo vi /etc/postgresql/9.5/main/postgresql.

Where is PostgreSQL data directory Ubuntu?

PostgreSQL configuration files are stored in the /etc/postgresql/<version>/main directory. For example, if you install PostgreSQL 12, the configuration files are stored in the /etc/postgresql/12/main directory. To configure IDENT authentication, add entries to the /etc/postgresql/12/main/pg_ident. conf file.


1 Answers

User postgres should have read, write and execute permissions on the data directory. No other user should have write access. You can set these permissions with:

chown -R postgres:postgres '/media/cesar/My Book/dataGE'
chmod -R u+rwx,g-rwx,o-rwx '/media/cesar/My Book/dataGE'

After changing the data directory, you should run initdb to set up the database files:

su - postgres
initdb
like image 59
Andomar Avatar answered Oct 19 '22 13:10

Andomar