1. 現象
# 直接psql 連接異常
postgres@s2ahumysqlpg01-> psql
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/u01/postgresql/data_bak/.s.PGSQL.5432"?
# psql -h 127.0.0.1 可以連接成功
psql: option requires an argument -- 'h'
Try "psql --help" for more information.
postgres@s2ahumysqlpg01-> psql -h 127.0.0.1
psql (12.4)
Type "help" for help.
postgres=#
2.問題分析
2.1 檢查 pg_hba.conf
cat pg_hba.conf
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
host replication repluser 192.168.1.0/24 md5
host all all 192.168.1.0/24 md5
host all all 0.0.0.0/24 md5
#無異常
2.2 檢查參數文件
postgres@s2ahumysqlpg01-> cat postgresql.conf postgresql.auto.conf |grep socket
#unix_socket_directories = '/tmp' # comma-separated list of directories
#unix_socket_group = '' # (change requires restart)
#unix_socket_permissions = 0777 # begin with 0 to use octal notation
#默認路徑未改動 ,但是 缺指向"/u01/postgresql/data_bak/.s.PGSQL.5432"?
懷疑是環境變量的問題
2.3 松果文件 路徑
# 檢查並沒有該文件
ll /u01/postgresql/data_bak/.s.PGSQL.5432
ll /tmp/.s.PGSQL.5432
2.4 檢查環境變量
cat .bash_profile
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
export PS1="$USER@`/bin/hostname -s`-> "
export PGPORT=5432
export PGDATA=/u01/postgresql/data_bak
export LANG=en_US.utf8
export PGHOME=/u01/postgresql/pg12
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export DATE=`date +"%Y%m%d%H%M"`
export PATH=$PGHOME/bin:/u01/mysql/app/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
export PGHOST=$PGDATA
export PGDATABASE=postgres
#export PATH=/home/postgres/pgbackrest/bin:$PATH:.
alias rm='rm -i'
# 發現設置了PGHOST
3. 處理
3.1 注釋環境變量 PGHOST ,並使期生效
cat .bash_profile
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
export PS1="$USER@`/bin/hostname -s`-> "
export PGPORT=5432
export PGDATA=/u01/postgresql/data_bak
export LANG=en_US.utf8
export PGHOME=/u01/postgresql/pg12
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export DATE=`date +"%Y%m%d%H%M"`
export PATH=$PGHOME/bin:/u01/mysql/app/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
#export PGHOST=$PGDATA
export PGDATABASE=postgres
alias rm='rm -i'
# 使環境變量生效
. .bash_profile
# 檢查
postgres@s2ahumysqlpg01-> env | grep PGHOST
postgres@s2ahumysqlpg01-> echo $PGHOST
postgres@s2ahumysqlpg01->
3.3 檢查文件
# 檢查文件是否存在
postgres@s2ahumysqlpg01-> ll /tmp/.s.PGSQL.5432
srwxrwxrwx. 1 postgres postgres 0 Feb 24 13:18 /tmp/.s.PGSQL.5432
3.3 重啟數據庫並測試
# 重啟數據庫 路徑指向了 /tmp/.s.PGSQL.5432
postgres@s2ahumysqlpg01-> pg_ctl restart -D /u01/postgresql/data_bak
waiting for server to shut down....... done
server stopped
waiting for server to start....LOG: Auto detecting pg_stat_kcache.linux_hz parameter...
LOG: pg_stat_kcache.linux_hz is set to 500000
LOG: starting PostgreSQL 12.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44.0.3), 64-bit
LOG: listening on IPv4 address "0.0.0.0", port 5432
LOG: listening on IPv6 address "::", port 5432
LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
LOG: redirecting log output to logging collector process
HINT: Future log output will appear in directory "log".
done
server started
# psql 連接測試,問題解決
postgres@s2ahumysqlpg01-> psql
psql (12.4)
Type "help" for help.
postgres=# quit
參考
https://www.cnblogs.com/ios9/p/15544892.html
https://blog.csdn.net/lyp13696402570/article/details/120769088?spm=1001.2014.3001.5501