由於環境變量 PGHOST配置不當引起的
postgres@pgdb-> psql
psql: 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.1921"?
若環境變量PGHOST未配置,且psql不加-h命令的時候,則默認使用的是數據庫參數unix_socket_directories 的默認值(一般是/tmp)
作為unix-domain-socket路徑。
即:psql 相當於 psql -h /tmp
情況1
若環境變量PGHOST未配置,並修改了unix_socket_directories參數,則直接使用psql命令連接會報錯:
注:postgres server 是運行狀態
postgres@pgdb-> psql
psql: 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.1921"
解決方法:
1.psql命令必須加 -h 選項 並指定 unix-domain-socket的路徑(即unix_socket_directories參數所設置的值)
2.或者環境變量里添加PGHOST,其值為unix_socket_directories 路徑
過程演示
1.無意修改了unix_socket_directories的默認值,並將其改成了任意目錄(這里假設將該參數值設置成/home/postgres/pgdata) 2.則下次使用直接使用psql連接時,便會報錯: postgres@pgdb-> psql psql: 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.1921"? 3.這時,解決方法如下: postgres@pgdb-> psql -h /home/postgres/pgdata psql (9.5.7) Type "help" for help. postgres=# 或者在環境變量里添加: export PGHOST=/home/postgres/pgdata 然后應用環境變量,再使用psql直連: postgres@pgdb-> psql psql (9.5.7) Type "help" for help. postgres=#