【问题标题】:How do I get a Postgres server to createdb and shut down?如何让 Postgres 服务器创建并关闭?
【发布时间】:2014-01-14 06:41:41
【问题描述】:

我有一个正在运行的 Postgres 服务器实例,我使用以下命令启动:

pg_ctl -D /usr/local/var/postgres/data -l /usr/local/var/postgres/data/server.log start

运行命令createdb test 两次提示我输入密码,然后我收到此错误:

createdb: could not connect to database template1: FATAL:  password authentication failed for user "joey" 

另外,当我尝试使用

停止服务器时
pg_ctl -D /usr/local/var/postgres/data stop -m smart

我收到此错误消息:

pg_ctl: PID file "/usr/local/var/postgres/data/postmaster.pid" does not exist
Is server running?

有什么我遗漏或忘记初始化/安装的吗?我用these instructions安装。

我检查了this answerthis answer,但两者都没有解决我的问题。

【问题讨论】:

标签: postgresql


【解决方案1】:

从您在评论中添加的日志文件来看,似乎机器上正在运行另一个 postgresql 实例(或者可能使用与 postgresql 想要使用的端口相同的其他实例):

LOG:  could not bind IPv6 socket: Address already in use
HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG:  could not bind IPv4 socket: Address already in use
HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG:  could not bind IPv6 socket: Address already in use
HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
WARNING:  could not create listen socket for "localhost"
FATAL:  could not create any TCP/IP sockets

这将阻止数据库启动。

要查看可能是什么进程,可以使用 lsof:

$ sudo lsof | grep -i listen
...
postmaste 19732  postgres    3u     IPv6            1194355       0t0        TCP localhost:postgres (LISTEN)
postmaste 19732  postgres    4u     IPv4            1194356       0t0        TCP localhost:postgres (LISTEN)
...

在上面的示例中,从 Linux 主机中,您可以看到一个名为 postmaster 的进程(在打印输出中被截断)侦听 localhost:postgres,这意味着 localhost 地址端口 5432(lsof 正在将端口 5432 转换为 ' postgres' 通过文件 /etc/services 包含众所周知的端口号和相应服务之间的映射)。

createb 提示您输入密码的事实表明它正在连接到某个地方的数据库,尽管我无法在您发送的 ps 打印输出中发现它。

您问题的另一部分是为什么 createdb 无法连接到您的数据库(或您机器上正在运行的任何数据库)。如果它是一个新创建的数据库集群,那么除了默认的“postgres”用户之外,它不会定义任何用户。您必须向该用户发出命令:

createdb -U postgres test

如果没有 -U 选项,它将尝试使用您当前的登录用户进行连接,该用户在数据库中不存在。

您也可能需要以 postgres 用户身份进行身份验证。 postgresql 数据目录中的文件pg_hba.conf 控制需要什么样的身份验证。

总的来说,postgresql 文档非常好;我建议您通读Server Setup and Operation 部分以检查您的配置是否有效。

【讨论】:

  • 我能够使用-U 标志运行createdb 命令。我无法在端口 5432 上找到阻塞操作,尽管我确实在端口 2657 上找到了一个由 postgres 进程运行的 postgres 服务器。另外两个注意事项: 1. 我按照您链接到的 Postgres 文档进行操作,并使用 su postgres -c 启动服务器并收到此错误:pg_ctl: could not open PID file "/usr/local/var/postgres/data/postmaster.pid": Permission denied。 2. 这里是一个链接,内容为pg_hba.conf。我需要进行任何更改吗?
  • 1.这意味着 postgres 'nix 用户不拥有数据目录(或其中的某些文件)。使用 'chown -R postgres:postgres /usr/local/var/postgres/data' 修复(以 root 身份)。 2. pg_hba.conf 可以测试,只要你的机器没有暴露。在对数据库做任何严肃的事情之前,你应该收紧它,因为它允许任何人在没有密码的情况下访问数据库。
  • 尝试运行chown -R postgres:postgres /usr/local/var/postgres/data并得到chown: postgres: illegal group name,然后运行'chown -R postgres :postgres /usr/local/var/postgres/data1postgres:postgres之间有一个空格)并得到很多“不允许操作”。
  • ok,不带组名试试:chown -R postgres /usr/local/var/postgres/data
  • 仍然得到“不允许操作”。有任何想法吗?顺便说一句,感谢您迄今为止的所有帮助。
猜你喜欢
  • 1970-01-01
  • 2013-02-03
  • 1970-01-01
  • 2011-09-13
  • 2022-10-14
  • 1970-01-01
  • 2010-09-21
  • 2021-09-28
相关资源
最近更新 更多