PostgreSQL连接被拒绝

11

我已经安装了PostgreSQL。但是每次我尝试通过PGAdmin或psql连接时,都会出现以下错误。

could not connect to server: Connection refused (0x0000274D/10061) 
      Is  the server running on host "localhost" (::1) and 
      accepting TCP/IP  connections on port 5432? 
could not connect to server: Connection  refused (0x0000274D/10061) 
      Is the server running on host "localhost" (127.0.0.1) and 
      accepting TCP/IP connections on port 5432?

我检查了 postgresql.conf 文件,发现这一行没有被注释掉:listen_addresses = '*'

此外,我的 pg_hba.conf 文件是这样设置的:

# IPv4 local connections:
  host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
  host    all             all             ::1/128                 trust

有谁能给我建议,告诉我如何解决这个问题吗?

谢谢!


2
pg_hba.conf, not pg_hba.conf.txt - Roman Tkachuk
6个回答

8

请使用“开始 -> 运行 -> services.msc”命令打开服务列表,查找postgresql-[vers]服务。

如果该服务未在运行,请尝试启动它。如果无法启动,请打开事件查看器(开始 -> 运行 -> eventvwr),查找与PostgreSQL服务相关的错误消息。


1
在寻找多个答案后,这个对我有效!只需要启动应用程序(右键单击-> 启动),然后错误就消失了! - Albert Israel

1

在我的情况下,是postgres.config文件出了问题。端口号被修改为5433,我不记得自己做过这样的操作。因此,请确保您的项目与postgres.config文件中的端口号匹配。


如果您在已经安装了PostgreSQL的情况下安装了第二个版本(可能是在尝试从X版本迁移到Y版本时),则可能会发生这种情况,因为第二个版本将看到已经安装并使用端口5432的另一个PostgreSQL,因此它会自动在postgres.conf中将其端口设置为5433,以便在两个服务器都运行时不会与其他PostgreSQL发生冲突。 - Nick Muise

0
也许你可以尝试这个。 打开命令提示符并插入。
"C:\Program Files\PostgreSQL\11\bin\pg_ctl.exe" runservice -N "postgresql-x64-11" -D "C:\Program Files\PostgreSQL\11\data" -w

按下回车键...


0

这对我有用 -

In C:\Program Files\PostgreSQL\data\postgresql.conf set listen_addresses ='localhost'

然后尝试 -

pg_ctl -D "C:\Program Files\PostgreSQL\9.5\data" start

如果已经尝试过,请使用pg_ctl重新启动


0

这对我有效。 记住,-D 后面的任何内容都应该是您安装 PostgreSQL 的路径,数据文件夹中包含 pg_hba.conf 和 postgresql.conf 文件。

pg_ctl start -D "C:/Program Files/PostgreSQL/9.6/data"

0

pg_hba.conf.txt 必须被命名为 pg_hba.conf

此外,当您遇到身份验证错误时,您将收到类似以下的消息:

psql -U nonexistent
psql: FATAL:  Peer authentication failed for user "nonexistent"

你遇到的错误很可能意味着在此服务器上未启动PostgreSQL。

您可以使用以下命令启动PostgreSQL:

service postgresql start
service postgresql status

1
psql -U myuser -d mydatabase 运行正常,我已经启动了服务并且 status 显示为 active。但是我仍然遇到了 Connection refused 错误。 - DarksteelPenguin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接