createuser 无法连接到数据库 postgres

createuser could not connect to database postgres

请不要将此问题移至 askubuntu,因为我认为此问题不是 OS 特定的。

当我调用 createuser postgres 命令时(现在我是否提供任何参数都无关紧要),我收到此错误:

createuser: could not connect to database postgres: 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 服务器已关闭但这次没有:

pg_lsclusters
Ver Cluster Port Status Owner    Data directory               Log file
9.4 main    5432 online postgres /var/lib/postgresql/9.4/main /var/log/postgresql/postgresql-9.4-main.log

sudo service postgresql status
9.4/main (port 5432): online

但是确实没有/tmp/.s.PGSQL.5432文件,因为我的配置文件(/etc/postgresql/9.4/main/postgresql.conf)有这一行:

unix_socket_directories = '/var/run/postgresql'

所以我不太明白为什么 createuser 想要访问 /tmp/.s.PGSQL.5432?可以将此路径硬编码到 createuser 二进制文件中吗?我没有看到任何命令行参数来指定 createuser...

的设置文件位置

postgresql.conf 文件由数据库 服务器 读取,但不由客户端应用程序(例如 createuser、psql 等)读取。 (实际上,客户端应用程序 无法 读取服务器配置文件,因为客户端必须先连接到服务器,然后才能知道服务器在哪里配置文件存在)。

相反,您必须告诉您的客户端 应用程序在哪里可以找到套接字目录。

如果您的客户端应用程序 (createuser) 正在连接到本地主机(这一定是因为您没有指定不同的主机),您可以使用主机参数来指定套接字目录的名称。

例如: createuser -h /var/run/postgresql newusername

http://www.postgresql.org/docs/devel/static/libpq-connect.html#LIBPQ-CONNECT-HOST

希望对您有所帮助。

你开启服务了吗?

service postgresql start