Postgresql

更改數據目錄後 psql 無法連接到本地數據庫

  • July 15, 2022

我有一個在 Linux 實例上執行的 PostgreSQL 12 數據庫。當我創建使用者時,user我忘記創建同名數據庫。一切正常,直到我data_directorypostgresql.conf文件更改為使用/mnt. 現在我無法訪問我的數據庫。

任何psql命令(即createdb)現在都會產生:

root@ubuntu-s-2vcpu-4gb-intel-fra1-01:~# createdb
createdb: error: could not connect to database template1: 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"?

即使使用 重新啟動後sudo systemctl restart postgresql,我的集群似乎也沒有執行:

root@ubuntu-s-2vcpu-4gb-intel-fra1-01:~# pg_lsclusters
Ver Cluster Port Status Owner Data directory                         Log file
12  main    5432 down   root  /mnt/volume_fra1_01/postgresql/12/main /var/log/postgresql/postgresql-12-main.log

日誌文件似乎表明我需要創建user數據庫,但如果我的數據庫沒有執行,我就無法做到:

的輸出/var/log/postgresql/postgresql-12-main.log

2022-07-13 22:39:20.179 UTC [27361] LOG:  starting PostgreSQL 12.11 (Ubuntu 12.11-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit
2022-07-13 22:39:20.179 UTC [27361] LOG:  listening on IPv6 address "::1", port 5432
2022-07-13 22:39:20.179 UTC [27361] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2022-07-13 22:39:20.181 UTC [27361] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-07-13 22:39:20.193 UTC [27362] LOG:  database system was shut down at 2022-07-13 22:39:19 UTC
2022-07-13 22:39:20.198 UTC [27361] LOG:  database system is ready to accept connections
2022-07-15 02:48:45.501 UTC [45714] topcat@topcat FATAL:  database "topcat" does not exist
2022-07-15 03:08:32.272 UTC [27361] LOG:  received fast shutdown request
2022-07-15 03:08:32.274 UTC [27361] LOG:  aborting any active transactions
2022-07-15 03:08:32.275 UTC [27361] LOG:  background worker "logical replication launcher" (PID 27368) exited with exit code 1
2022-07-15 03:08:32.279 UTC [27363] LOG:  shutting down
2022-07-15 03:08:32.295 UTC [27361] LOG:  database system is shut down
~

即使我更改為postgres使用者,也會出現同樣的問題。我應該把它全部燒掉並重新開始嗎?我在這裡沒有想法。

root在 pg_lsclustersOwner列中看起來不對。數據目錄的所有者必須是postgres. 它的權限也必須是0700.

如果數據目錄的所有者或權限異常,Postgres 服務將不會啟動。它可能會在伺服器日誌中抱怨它,但也許此檢查發生得太快以至於它此時尚未將輸出重定向到日誌文件。

也許journalctl -xe會顯示一些有關您何時執行sudo systemctl restart postgresql但無法啟動的詳細資訊。

引用自:https://dba.stackexchange.com/questions/314459