简体   繁体   中英

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

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

I would like to share my issue basically was with permission. DB Cluster was not starting when I check with this >> pg_lsclusters when I check the Log file >> sudo nano /var/log/postgresql/postgresql-9.6-main.log "FATAL: data directory "/var/lib/postgresql/9.6/main/" has group or world access DETAIL: Permissions should be u=rwx (0700) "

The solution is already mentioned so I just executed >> sudo chmod -R u=rwx '/var/lib/postgresql/9.6/main/' and restarted postgresql service {notable took some time to start which was good indication of doing something} after that I check with >> pg_lsclusters and YES it worked!!

Adding to @Jameel's & @Andomar's answers (as i had this issue on my local postgres db),

My setup is a local Postgres DB . And I've enabled db logging to see what queries are running on the db and their errors (Just FYI... I've a hibernate application persisting to db on backend and to debug hibernate issues, I felt its easy to check postgres logs instead of juggling with logger configuration).

postgres logs are in /9.6/Data/logs. To open the log with an editor (eg: sublime text) I added read-only permission to everyone. though it didn't readily stopped my postgres server, upon subsequent restart attempts (for eg restart laptop) postgres server was failing to start.

so removing all the permissions to everyone recursively on /9.6/Data resolved my issue. I mean it should be read & write to postgres user only and its a must that no other permission.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM