简体   繁体   English

如何获得Postgres服务器以创建并关闭?

[英]How do I get a Postgres server to createdb and shut down?

I have an instance of a Postgres server running that I've started with the command: 我有一个正在运行的Postgres服务器实例,该实例已通过以下命令启动:

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

Running the command createdb test prompts me for my password twice, then I get this error: 运行命令createdb test会提示我输入两次密码,然后出现此错误:

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

Also, when I try to stop the server using 另外,当我尝试使用停止服务器

pg_ctl -D /usr/local/var/postgres/data stop -m smart

I get this error message: 我收到此错误消息:

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

Is there something I'm missing or forgot to initialize/install? 我缺少什么或忘记初始化/安装了吗? I used these instructions to install. 我按照这些说明进行安装。

I checked this answer and this answer and neither of the two fixed my problem. 我检查了这个答案这个答案 ,两个都没有解决我的问题。

From the log file you added in your comment, it looks as if either there is another postgresql instance running on the machine (or possibly something else which is using the same port as postgresql wants to use): 从您在注释中添加的日志文件中,看起来好像机器上正在运行另一个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

This is stopping the database from starting up to begin with. 这将阻止数据库启动。

To see what process that could be, you can use lsof: 要查看可能是什么过程,可以使用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)
...

In the above sample, from a Linux host, you can see that a process called postmaster (it is truncated in the printout) listens on localhost:postgres, meaning the localhost address port 5432 (lsof is translating the port 5432 into 'postgres' via the file /etc/services which contains a mapping between well known port numbers and corresponding services). 在上面的示例中,从Linux主机上可以看到名为postmaster的进程(在打印输出中被截断)在localhost:postgres上侦听,这意味着localhost地址端口5432(lsof通过以下方式将端口5432转换为“ postgres”)文件/ etc / services,其中包含众所周知的端口号和相应服务之间的映射)。

The fact that createdb is prompting for your password implies that it is connecting to a database somewhere, although I could not spot it the ps printout you sent. createdb提示您输入密码的事实意味着它正在连接到某个地方的数据库,尽管我无法发现您发送的ps打印输出。

The other part of your question was why createdb could not connect to your database (or whatever database is running on your machine). 问题的另一部分是为什么createdb无法连接到数据库(或计算机上正在运行的任何数据库)。 If it is a freshly created database cluster then it will not have any users defined other than the default 'postgres' user. 如果它是新创建的数据库集群,则除了默认的“ postgres”用户外,将没有其他用户定义。 You must issue commands with this user: 您必须对此用户发出命令:

createdb -U postgres test

Without the -U option it will try to connect using your current login user, which won't exist in the database. 如果没有-U选项,它将尝试使用您当前的登录用户进行连接,该用户在数据库中不存在。

It might also be that you will need to authenticate as the postgres user. 也可能需要您以postgres用户身份进行身份验证。 The file pg_hba.conf in the postgresql data directory controls what kind of authentication will be needed. postgresql数据目录中的文件pg_hba.conf控制将需要哪种身份验证。

In general the postgresql documentation is excellent; 总的来说,postgresql文档非常出色。 I suggest you read through the section Server Setup and Operation to check that you have a valid configuration. 建议您通读“ 服务器设置和操作 ”部分以检查您的配置是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM