postgresql Psql 无法连接到服务器:没有那个文件或目录,5432 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42653690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:23:58  来源:igfitidea点击:

Psql could not connect to server: No such file or directory, 5432 error?

postgresqlvagrant

提问by Sa?a Kalaba

I'm trying to run psqlon my Vagrant machine, but I get this error:

我正在尝试psql在我的 Vagrant 机器上运行,但出现此错误:

psql: could not connect to server: No such file or directory

Is the server running locally and accepting connections on 
Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Note: Vagrant 1.9.2 Box: ubuntu/trusty64, https://atlas.hashicorp.com/ubuntu/boxes/trusty64

注:Vagrant 1.9.2 Box:ubuntu/trusty64,https://atlas.hashicorp.com/ubuntu/boxes/trusty64

EDITCommands I've used in order to install and run postgres:

我用来安装和运行 postgres 的编辑命令:

  • sudo apt-get update
  • sudo apt-get install postgresql
  • sudo su postgres
  • psql -d postgres -U postgres
  • sudo apt-get update
  • sudo apt-get install postgresql
  • sudo su postgres
  • psql -d postgres -U postgres

回答by malvadao

I've had this same issue, related to the configuration of my pg_hba.conf file (located in /etc/postgresql/9.6/main). Please note that 9.6 is the postgresql version I am using.

我遇到了同样的问题,与我的 pg_hba.conf 文件(位于/etc/postgresql/9.6/main)的配置有关。请注意,9.6 是我使用的 postgresql 版本。

The error itself is related to a misconfiguration of postgresql, which causes the server to crash before it starts.

该错误本身与 postgresql 的错误配置有关,这会导致服务器在启动之前崩溃。

I would suggest following these instructions:

我建议遵循以下说明:

  1. Certify that postgresql service is running, using sudo service postgresql start
  2. Run pg_lsclustersfrom your terminal
  3. Check what is the cluster you are running, the output should be something like:

    Version - Cluster Port Status Owner Data directory

    9.6 ------- main -- 5432 online postgres /var/lib/postgresql/9.6/main

    Disregard the '---' signs, as they are being used there only for alignment. The important information are the version and the cluster. You can also check whether the server is running or not in the status column.

  4. Copy the info from the version and the cluster, and use like so: pg_ctlcluster <version> <cluster> start, so in my case, using version 9.6 and cluster 'main', it would be pg_ctlcluster 9.6 main start
  5. If something is wrong, then postgresql will generate a log, that can be accessed on /var/log/postgresql/postgresql-<version>-main.log, so in my case, the full command would be sudo nano /var/log/postgresql/postgresql-9.6-main.log.
  6. The output should show what is the error.

    2017-07-13 16:53:04 BRT [32176-1] LOG: invalid authentication method "all"
    2017-07-13 16:53:04 BRT [32176-2] CONTEXT: line 90 of configuration file "/etc/postgresql/9.5/main/pg_hba.conf"
    2017-07-13 16:53:04 BRT [32176-3] FATAL: could not load pg_hba.conf

  7. Fix the errors and restart postgresql service through sudo service postgresql restartand it should be fine.
  1. 证明 postgresql 服务正在运行,使用 sudo service postgresql start
  2. pg_lsclusters从终端运行
  3. 检查您正在运行的集群是什么,输出应该是这样的:

    版本 - 集群端口状态所有者数据目录

    9.6 ------- main -- 5432 在线 postgres /var/lib/postgresql/9.6/main

    忽略“---”符号,因为它们仅用于对齐。重要信息是版本和集群。您还可以在状态栏中检查服务器是否正在运行。

  4. 从版本和集群复制信息,然后像这样使用: pg_ctlcluster <version> <cluster> start,所以在我的情况下,使用版本 9.6 和集群“main”,它将是pg_ctlcluster 9.6 main start
  5. 如果出现问题,则 postgresql 将生成一个日志,可以在 上访问该日志/var/log/postgresql/postgresql-<version>-main.log,因此在我的情况下,完整命令将是sudo nano /var/log/postgresql/postgresql-9.6-main.log.
  6. 输出应该显示什么是错误。

    2017-07-13 16:53:04 BRT [32176-1] LOG: 无效的身份验证方法“all”
    2017-07-13 16:53:04 BRT [32176-2]上下文:配置文件“/etc”的第 90 行/postgresql/9.5/main/pg_hba.conf"
    2017-07-13 16:53:04 BRT [32176-3] 致命:无法加载 pg_hba.conf

  7. 修复错误并通过重新启动 postgresql 服务sudo service postgresql restart,它应该没问题。

I have searched a lot to find this, credit goes to this post.

我已经搜索了很多找到这个,功劳归于这篇文章

Best of luck!

祝你好运!

回答by martinlasek

I had the same issue but non of the answers here helped.

我有同样的问题,但这里的答案都没有帮助。

How I fixed it (mac)

我是如何修复它的 (mac)

  • Try to start postgresql with pg_ctl -D /usr/local/var/postgres start
  • Look for the Error Messagethat says something like FATAL: could not open directory "pg_tblspc": No such file or directory.
  • Create that missing directory mkdir /usr/local/var/postgres/pg_tblspc
  • Repeat from step one until you created all missing directories
  • When done and then trying to start postgresql again it mightsay FATAL: lock file "postmaster.pid" already exists
  • Delete postmaster.pid: rm /usr/local/var/postgres/postmaster.pid
  • Start postgres with: pg_ctl -D /usr/local/var/postgres start
  • Done ?
  • 尝试启动 postgresql pg_ctl -D /usr/local/var/postgres start
  • 查找错误消息说像FATAL: could not open directory "pg_tblspc": No such file or directory
  • 创建丢失的目录 mkdir /usr/local/var/postgres/pg_tblspc
  • 从第一步开始重复,直到创建所有丢失的目录
  • 完成后尝试再次启动 postgresql,它可能会FATAL: lock file "postmaster.pid" already exists
  • 删除postmaster.pidrm /usr/local/var/postgres/postmaster.pid
  • 用以下命令启动 postgres: pg_ctl -D /usr/local/var/postgres start
  • 完毕 ?

回答by wfgeo

I am just posting this for anyone who is feeling lost and hopeless as I did when I found this question. It seems that sometimes by editing some psotgresql-related config files, one can accidentally change the permissions of the file:

我只是为那些感到迷茫和绝望的人发布这个,就像我发现这个问题时所做的那样。似乎有时通过编辑一些与 psotgresql 相关的配置文件,可能会意外更改文件的权限:

enter image description here

enter image description here

Note how pg_hba.conf belongs to root, and users cannot even read it. This causes postgres to not be able to open this file and therefore not be able to start the server, throwing the error seen in the original question.

注意 pg_hba.conf 是如何属于 root 的,用户甚至无法读取它。这会导致 postgres 无法打开此文件,因此无法启动服务器,从而引发在原始问题中看到的错误。

By running

通过跑步

sudo chmod +r pg_hba.conf

sudo chmod +r pg_hba.conf

I was able to make this file once again accessible to the postgres user and then after running

我能够让 postgres 用户再次访问这个文件,然后在运行之后

sudo service postgresql start

sudo service postgresql start

Was able to get the server running again.

能够让服务器再次运行。

回答by J2112O

Does the /etc/postgresql/9.6/main/postgresql.confshow that port being assigned? On my default Xubuntu Linux install, mine showed port = 5433 for some reason as best as I can remember, but I did comment out the line in that same file that said listen_addresses = 'localhost'and uncommented the line listen_addresses = '*'. So maybe start and check there. Hope that helps.

是否/etc/postgresql/9.6/main/postgresql.conf显示正在分配的端口?在我的默认 Xubuntu Linux 安装中,由于某种原因,我的显示 port = 5433 尽我所能,但我确实注释掉了同一个文件中的那行,listen_addresses = 'localhost'并取消了该行的注释listen_addresses = '*'。所以也许开始并检查那里。希望有帮助。

回答by Gaurav Verma

Use command:

使用命令:

rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8

回答by Tucker Watts

Within zsh:

在 zsh 中:

rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8

This is the only thing that worked for me after countless hours trouble shooting.

在无数小时的故障排除后,这是唯一对我有用的方法。

回答by Mevlüt?zdemir

This works for me:

这对我有用:

pg_ctl -D /usr/local/var/[email protected] stop;
brew services stop [email protected];
brew services start [email protected];

回答by xji

In my case it was the lockfile postmaster.idthat was not deleted properly during the last system crash that caused the issue. Deleting it with sudo rm /usr/local/var/postgres/postmaster.pidand restarting Postgres solved the problem.

就我而言,是postmaster.id上次系统崩溃期间未正确删除的锁定文件导致了问题。删除它sudo rm /usr/local/var/postgres/postmaster.pid并重新启动 Postgres 解决了这个问题。

回答by Nasser Abdou

Open your database manager and execute this script

打开您的数据库管理器并执行此脚本

update pg_database set datallowconn = 'true' where datname = 'your_database_name';

回答by coderanger

The same thing happened to me as I had changed something in the /etc/hostsfile. After changing it back to 127.0.0.1 localhostit worked for me.

同样的事情发生在我身上,因为我更改了/etc/hosts文件中的某些内容。将其改回后,127.0.0.1 localhost它对我有用。