作者:瀚高PG实验室 (Highgo PG Lab)- 天蝎座
unix_socket_directories参数指定服务器侦听客户端应用程序连接的Unix域套接字的目录。默认值通常是/tmp,但可以更改。此参数只能在服务器启动时设置。
除了名为.s.PGSQL.nnnn的套接字文件本身之外,其中nnnn是服务器的端口号,将在每个unix_socket_directories目录中创建一个为.s.PGSQL.nnnn.lock的普通文件。 这两个文件都不应该手动删除。
但是假如被误删除了怎么办?
做个测试:
[postgres@hgdb tmp]$ rm -rf .s.PGSQL.5432.lock
[postgres@hgdb tmp]$ psql
psql (10.0)
Type "help" for help.
postgres=# \q
[postgres@hgdb tmp]$ psql -h 127.0.0.1
psql (10.0)
Type "help" for help.
postgres=# \q
[postgres@hgdb tmp]$ rm -rf .s.PGSQL.5432
[postgres@hgdb tmp]$ 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.5432"?
[postgres@hgdb tmp]$ psql -h 127.0.0.1
psql (10.0)
Type "help" for help.
postgres=#
[postgres@hgdb tmp]$ pg_ctl restart -m f
[postgres@hgdb tmp]$ ll -a
srwxrwxrwx 1 postgres postgres 0 Feb 7 15:09 .s.PGSQL.5432
-rw------- 1 postgres postgres 42 Feb 7 15:09 .s.PGSQL.5432.lock
从上面的实验可以看出,删除套接字文件后,重启数据库,又重新生成了套接字文件。在没有套接字文件的情况下,可以通过TCP/IP套接字连接数据库。
前面提到unix_socket_directories是可以修改的。假如将默认的/tmp 修改为/home/postgres 呢?
postgres=# show unix_socket_directories ;
unix_socket_directories
-------------------------
/tmp
(1 row)
postgres=# alter system set unix_socket_directories TO '/home/postgres';
ALTER SYSTEM
[postgres@hgdb tmp]$ pg_ctl restart -m f
[postgres@hgdb tmp]$ 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.5432"?
[postgres@hgdb tmp]$ psql -h 127.0.0.1
psql (10.0)
Type "help" for help.
postgres=# show unix_socket_directories ;
unix_socket_directories
-------------------------
/home/postgres
(1 row)
postgres=# \q
[postgres@hgdb tmp]$
如上所示:unix_socket_directories只影响使用psql走Unix域套接字连接数据库的情况。
[postgres@hgdb tmp]$ 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.5432"?
[postgres@hgdb tmp]$ psql -h /home/postgres/
psql (10.0)
Type "help" for help.
postgres=# \q
[postgres@hgdb tmp]$ ll -a /home/postgres/
srwxrwxrwx 1 postgres postgres 0 Feb 7 15:20 .s.PGSQL.5432
-rw------- 1 postgres postgres 52 Feb 7 15:20 .s.PGSQL.5432.lock