Postgres 无法连接到服务器

Postgres could not connect to server

提问人:Gary Lai 提问时间:11/16/2012 最后编辑:Nikita FedyashevGary Lai 更新时间:10/11/2023 访问量:597309

问:

在我做完之后,我的postgres遇到了一些问题。我尝试卸载postgres并重新安装它,但它不起作用。brew updatebrew upgrade

这是错误消息。(当我尝试这样做时,我也收到了此错误消息rake db:migrate)

$ psql
psql: 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"?

我该如何解决?

Mac版本:美洲狮。

自制版本:0.9.3

Postgres 版本:psql (PostgreSQL) 9.2.1

这就是我所做的:

$ brew uninstall postgresql
Uninstalling /usr/local/Cellar/postgresql/9.2.1...
$ brew uninstall postgresql
Uninstalling /usr/local/Cellar/postgresql/9.1.4...
$ psql --version
bash: /usr/local/bin/psql: No such file or directory
$ brew install postgresql
==> Downloading http://ftp.postgresql.org/pub/source/v9.2.1/postgresql-9.2.1.tar.bz2
Already downloaded: /Library/Caches/Homebrew/postgresql-9.2.1.tar.bz2
......
......
==> Summary
/usr/local/Cellar/postgresql/9.2.1: 2814 files, 38M, built in 2.7 minutes
$ initdb /usr/local/var/postgres -E utf8
The files belonging to this database system will be owned by user "laigary".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default text search configuration will be set to "english".

initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".
$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/postgresql/9.2.1/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
homebrew.mxcl.postgresql: Already loaded
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
server starting
$ env ARCHFLAGS="-arch x86_64" gem install pg
Building native extensions.  This could take a while...
Successfully installed pg-0.14.1
1 gem installed
$ psql --version
psql (PostgreSQL) 9.2.1
$ psql
psql: 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"?

现在,在我重新安装自制软件后,当我使用 时,它没有显示任何错误消息。$ psql

但是我在我的 Rails 应用程序中运行,它显示:rake db:migrate

could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `initialize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `new'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `connect'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:329:in `initialize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:309:in `new_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:319:in `checkout_new_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:241:in `block (2 levels) in checkout'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `loop'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `block in checkout'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:233:in `checkout'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:96:in `block in connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:404:in `retrieve_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:170:in `retrieve_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:144:in `connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:107:in `rescue in create_database'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:51:in `create_database'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `block (3 levels) in <top (required)>'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `each'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `block (2 levels) in <top (required)>'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:205:in `call'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:205:in `block in execute'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:200:in `each'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:200:in `execute'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:151:in `invoke_with_call_chain'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:144:in `invoke'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:116:in `invoke_task'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:94:in `block (2 levels) in top_level'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:94:in `each'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:94:in `block in top_level'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:88:in `top_level'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:66:in `block in run'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:63:in `run'
/usr/local/bin/rake:32:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"riy_development", "pool"=>5, "username"=>nil, "password"=>nil}

最后,我找到了解决方案。

sudo mkdir /var/pgsql_socket/
sudo ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/

这个解决方案有点棘手,但它有效。希望任何人都有更好的解决方案

更新

这也适用于我。

rm /usr/local/var/postgres/postmaster.pid
Ruby-on-Rails 数据库 PostgreSQL macOS 自制软件

评论

4赞 ajbraus 2/17/2013
我有完全相同的问题,但你的解决方案对我不起作用。我认为这是一个权限问题,但我不了解文件权限来修复它。
0赞 zquintana 10/27/2013
对于那些通过 Homebrew 安装 PG 并遇到问题的人,我找到了另一个答案。只需卸载 pg gem 并使用 Homebrew 配置重新安装即可。请参阅 stackoverflow.com/a/19609228/1072058 的答案。
0赞 VelLes 3/20/2014
类似的答案在这里 - stackoverflow.com/questions/13573204/...
2赞 atw 2/11/2017
启动 postgres。
21赞 4/6/2019
使用以下命令将现有版本升级到较新版本brew postgresql-upgrade-database

答:

17赞 jrwren 11/16/2012 #1

检查套接字文件是否存在。

$ ls -l /tmp/.s.PGSQL.5432
srwxrwxrwx  1 you  wheel  0 Nov 16 09:22 /tmp/.s.PGSQL.5432

如果没有,请检查您的 postgresql.conf 是否有unix_socket_directory更改。

$ grep unix_socket /usr/local/var/postgres/postgresql.conf
#unix_socket_directory = ''     # (change requires restart)
#unix_socket_group = ''         # (change requires restart)
#unix_socket_permissions = 0777     # begin with 0 to use octal notation

评论

1赞 Gary Lai 11/17/2012
这是我的电脑响应。$ ls -l /tmp/.s.PGSQL.5432 ls: /tmp/.s.PGSQL.5432: No such file or directory $ grep unix_socket /usr/local/var/postgres/postgresql.conf #unix_socket_directory = '' # (change requires restart) #unix_socket_group = '' # (change requires restart) #unix_socket_permissions = 0777 # begin with 0 to use octal notation
29赞 Gary Lai 11/18/2012
最后,我找到了解决方案。 这个解决方案有点棘手,但它有效。希望任何人都有更好的解决方案$ mkdir /var/pgsql_socket/$ sudo mkdir /var/pgsql_socket/$ ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/
0赞 jrwren 11/21/2012
干得不错@GaryLai。很奇怪,我不必对我的自制 postgresql 做同样的事情
2赞 AdamT 2/20/2013
这可能会对一些人有所帮助:github.com/mxcl/homebrew/issues/14527
36赞 David Winiecki 6/14/2013 #2

对于任何阅读本文并使用 Postgres.app 的人,您可能需要在database.yml。http://postgresapp.com/documentation#toc_3host: localhost

评论

0赞 Brett Bim 3/23/2014
这样做会将连接从套接字更改为 TCP 连接,这可能会产生负面影响。查看 stackoverflow.com/questions/6770649/...
1赞 David Winiecki 5/1/2014
嗯,实际上,如果你投反对票......也许不说为什么?meta.stackoverflow.com/questions/252297/......或者说为什么,但先注销,这样你就可以匿名了。
0赞 Brian Dear 11/16/2014
我认为投反对票是因为最初的问题不是针对 Postgres.app 而是针对 Homebrew。因此,虽然答案可能在某个地方有用,但它并没有回答问题。
0赞 sixty4bit 8/21/2017
该链接似乎没有具体到任何地方(过去搜索“localhost”没有结果),但经过一个小时的搜索,这为我解决了这个问题。
0赞 SocoM 7/22/2013 #3

原因

Lion 已经安装了 postgres 版本,并默认使用这些二进制文件。通常,您可以通过使用自制 postgres 二进制文件的完整路径来解决这个问题,但其他程序可能仍然存在问题。

解决方案

运行以下脚本:

#!/bin/sh

BREW_POSTGRES_DIR=`brew info postgres | awk '{print $1"/bin"}' | grep "/postgresql/"`
LION_POSTGRES_DIR=`which postgres | xargs dirname`
LION_PSQL_DIR=`which psql | xargs dirname`

sudo mkdir -p $LION_POSTGRES_DIR/archive
sudo mkdir -p $LION_PSQL_DIR/archive

for i in `ls $BREW_POSTGRES_DIR`
do
    if [ -f $LION_POSTGRES_DIR/$i ] 
    then
        sudo mv $LION_POSTGRES_DIR/$i $LION_POSTGRES_DIR/archive/$i
        sudo ln -s $BREW_POSTGRES_DIR/$i $LION_POSTGRES_DIR/$i
    fi
    
    if [ -f $LION_PSQL_DIR/$i ] 
    then
        sudo mv $LION_PSQL_DIR/$i $LION_PSQL_DIR/archive/$i
        sudo ln -s $BREW_POSTGRES_DIR/$i $LION_PSQL_DIR/$i
    fi  
done

通过

http://nextmarvel.net/blog/2011/09/brew-install-postgresql-on-os-x-lion/

7赞 John 8/24/2013 #4

更改 postresql 或 database.yml 配置设置、更改$PATH或创建符号链接对我来说都是不必要的。我需要做的就是然后(或)。gem uninstall pgbundlegem install pg

问题是 pg gem 是在自制 postgres 之前安装的,因此从 MacOS 附带的 postgres 版本中获取设置。重新安装它(从而重建本机扩展)解决了该问题。

评论

0赞 craig1410 2/13/2018
只是想说声谢谢。我刚刚在 macOS 上升级了 PostgresApp,虽然我可以使用 psql 连接,但我无法通过 rails 连接。卸载和重新安装 PG Gem 只是门票!:)
1252赞 jonathanpberger 9/16/2013 #5

有类似的问题;PID 文件阻止 Postgres 启动。要修复它,请执行以下操作:

$ rm /usr/local/var/postgres/postmaster.pid
$ brew services restart postgresql

然后一切都好了。


更新:

对于 Apple M1 (Big Sur) 用户,请改为执行此操作:

$ rm /opt/homebrew/var/postgres/postmaster.pid
$ brew services restart postgresql

更新(2022 年 11 月 15 日):

对于 Apple M1 / M2 (Ventura) 用户,只需这样做:

$ bundle install (just to make sure pg is installed)
$ brew services restart postgresql

评论

9赞 eprothro 12/11/2013
在更新/重新启动时看到此问题后,这在 OSX Mavericks 上对我有用。
3赞 betoharres 10/21/2014
使用优胜美地,它不起作用。仍然有同样的错误,文件不再存在。但它曾经为我适用于 OSX 版本......有什么帮助吗?“无法连接到服务器:没有这样的文件或目录 服务器是否在本地运行并接受 Unix 域套接字”/tmp/.s.PGSQL.5432“上的连接?
311赞 sscirrus 3/25/2015
这在 Mavericks 上对我不起作用:rm: /usr/local/var/postgres/postmaster.pid: No such file or directory
18赞 Ricardo Castañeda 3/27/2015
这对我有用,擦除后我不得不启动postgres。# pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log 启动
20赞 Josh Bodah 5/7/2015
我建议跟踪服务器日志,以确保这确实是问题所在:tail /usr/local/var/postgres/server.log
3赞 TomFuertes 1/25/2014 #6

在其他进程访问它们时尝试恢复/删除/创建数据库后,我遇到了这个问题。MacOSX/Homebrew 修复是:

  1. 关闭所有其他访问进程 , , , , ,rails serverrails consoleguard
  2. 使用以下命令进行加载/卸载brew info postgres
  3. 从之前运行 restore/drop/create
5赞 Juno Choo 3/2/2014 #7

Psql 选项

-h 主机名 --host=主机名

:指定运行服务器的计算机的主机名。如果该值以斜杠开头,则将其用作 Unix 域套接字的目录。

$ grep "port\|unix_socket" /etc/postgresql/9.1/main/postgresql.conf
port = 5433                                         # (change requires restart)
unix_socket_directory = '/var/run/postgresql'       # (change requires resta

$ netstat -nalp | grep postgres
unix  2      [ ACC ]     STREAM     LISTENING     106753   4349/postgres       /tmp/.s.PGSQL.5432
unix  2      [ ACC ]     STREAM     LISTENING     10377 1031/postgres       /var/run/postgresql/.s.PGSQL.5433

使用 -host 选项运行 psql

$ psql -p 5433 -h /var/run/postgresql

无需建立软链接

2赞 Benjamin Dunphy 5/11/2014 #8

我遇到了同样的错误。事实证明,postgres 根本没有运行(它通常总是在后台运行,但无论出于何种原因,今天都没有)。

如果是这种情况,只需在项目目录的命令行中键入即可postgres

2赞 Anthony 11/24/2014 #9

经过大量的来回折腾,它真的归结为我正在使用的宝石版本。在 mavericks 上,版本不会连接到端口 5432,但版本工作正常 - 非常奇怪。pgpg0.15.10.17.1

40赞 Ryan Atallah 12/18/2014 #10

在 Yosemite 上,如果 pid 文件阻止 Postgres 启动,并且您有一个守护程序尝试(但失败)加载数据库守护程序,那么您需要卸载 plist 文件:launchctl

$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

然后删除 pid 文件

$ rm /usr/local/var/postgres/postmaster.pid

然后重新加载守护程序launchctl

$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

评论

0赞 Goulven 1/23/2023
这也适用于蒙特利 MacOS!谢谢
364赞 FreePender 1/13/2015 #11

当 brew 进行 postgres 升级时,有时会发生这种情况,导致数据文件与新服务器不兼容。

就我而言,它发生在从 9.3 升级到 9.4 时。

OS X/自制软件:

尝试运行 - 如果 postgres 无法启动,它会给你一个更详细的输出。或者打开日志文件(在 Mac M1 上)并查找“致命:数据库文件与服务器不兼容”postgres -D /usr/local/var/postgres/usr/local/var/log/postgres.log/opt/homebrew/var/log/postgres.log

就我而言,运行删除了我的旧数据库,然后重新初始化了 postgres 数据库模式。(这将破坏您的数据)rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8

感谢 https://github.com/Homebrew/homebrew/issues/35240 提供该解决方案。可以在此处找到彻底重新安装的完整说明: 如何完全卸载并重新安装 Homebrew Postgres - Test Double Blog(同样,如果您使用的是 M1 Mac,请在 /usr/local/var 路径所在的位置替换 /opt/homebrew/var 路径)

重新生成我的数据库(使用 )后,一切又恢复了正常。rake db:create

最后,评论中的链接指向了这个可能的解决方案,可以保留您的数据,但我还没有尝试过: 如何在不丢失数据的情况下将 PostgreSQL 从版本 9.6 升级到版本 10.1 - Stack Overflow

评论

1赞 cpursley 2/2/2015
这对我有用,在优胜美地上安装了自制的 postgres。
7赞 Nathan Long 3/27/2015
如果运行告诉您数据目录是使用较旧的、不兼容的 Postgres 版本初始化的,并且您不想丢失本地数据,则可以尝试此线程postgres -D /usr/local/var/postgres
5赞 Mike Andrianov 4/8/2016
在 OSX 上使用在后台启动 postgres,而不是brew services start postgrespostgres -D /usr/local/var/postgres
1赞 stujo 8/11/2016
请注意,这会删除您的数据库,这可能不是您想要的(或者您可能不在乎这是否一直都是开发)
1赞 Micah Walter 2/28/2018
@coffekid我遇到了同样的问题,并按照此处的说明解决了它:stackoverflow.com/questions/24379373/......
0赞 Jayakumar Jai J Elumalai 1/24/2015 #12

安装后只需两个步骤即可运行数据库(在此之前,请确保您的登录身份)postgres

已安装-dir/bin/postmaster -D 已安装-dirs/pgsql/data

举个例子:

[postgres@localhost bin]$ /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data

步骤2:从安装路径运行psql(检查安装位置“#which postgres”将用于查找安装位置)

[postgres@localhost bin]$ psql 
3赞 Ruy Diaz 2/10/2015 #13

FWIW 这发生在我身上,但发生的事情是我当时正在运行 Ubuntu 更新,这些更新可能正在更新 Postgres。更新完成后,我能够顺利连接。

为了完整起见,我试图从 Rails 控制台从数据库中检索记录:

development (main):0 > a = MyModel.find 73694
PG::ConnectionBad: 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"?
91赞 Mosab Sasi 4/1/2015 #14

在这里找到了适合我的解决方案:

https://dba.stackexchange.com/questions/75214/psql-could-not-connect-to-server-no-such-file-or-directory

基本上,您可以运行以下命令来手动启动服务器:

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

评论

10赞 Patrick G. 8/25/2016
在所有答案中,这是唯一对我有用的答案。使用 El Capitan
0赞 Harshit Trivedi 7/20/2019
我试过用那个cmd,但对我不起作用!
3赞 lisyk 6/10/2015 #15

看起来你的psql没有运行。您应该在连接之前运行它。您只能使用 Postgres.app for Mac OS 执行此操作。(下载并安装此应用程序 http://postgresapp.com)打开应用程序,您已经准备好了 PostgreSQL 服务器并等待新连接。关闭应用程序,服务器将关闭。您也可以在此处找到此信息 http://www.postgresql.org/download/macosx/。希望这对你有所帮助。

评论

1赞 Kmeixner 6/10/2015
虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接的页面发生更改,仅链接的答案可能会失效。
0赞 Brian Dear 8/27/2015
该问题特指自制安装,而不是 Postgres.app 安装。虽然这个答案很有帮助,但它并没有回答这个问题。这就像说“改用MySQL”。
7赞 stujo 8/11/2016 #16

当我从 9.3.4 升级到 9.5 时,这发生在我身上,因为数据库不兼容而不升级。

我使用pg_upgrade如下:

停止 postgres

$ brew services stop postgresql

升级数据库:

$ pg_upgrade \
   -d /usr/local/var/postgres \
   -D /usr/local/var/postgres9.5 \
   -b /usr/local/Cellar/postgresql/9.3.4/bin/ \
   -B /usr/local/Cellar/postgresql/9.5.0/bin/ \
   -v

存档旧数据库:

 $ mv /usr/local/var/postgres /usr/local/var/postgres9.3.save
 $ mv /usr/local/var/postgres9.5 /usr/local/var/postgres

重新启动 postgres:

 $ brew services start postgresql

更新的 Gem(用于 rails / 活动记录):

 $ gem uninstall pg
 $ gem uninstall activerecord-postgresql-adapter
 $ bundle install

评论

0赞 Jan Hettich 7/22/2017
这些说明也在 collectiveidea.com/blog/archives/2016/01/08/...,今天从 9.4.0 升级到 9.6.3 时仍然对我有用。我不需要手动对 .pid 文件执行任何操作。
3赞 Kamara Jōb 10/1/2016 #17

这是因为以前的服务器仍在运行,请尝试关闭所有内容并重新运行您的应用程序。

2赞 kaah 10/11/2016 #18

对我来说,是apache升级导致了问题。我仍然可以在控制台中运行 psql 或直接从 kdevelop 调用 db。此外,它还可以将“host=localhost”添加到连接字符串中。

但真正的问题是 apache 已经更改为私有 tmp。

溶液:更新 /usr/lib/systemd/system/apache2.service 并将 PrivateTmp=true 更改为 PrivateTmp=false。

我正在开发 OpenSuse OS,但我想在 Mac 上可能会发生类似的事情。

4赞 Austin 11/3/2016 #19

在MacOS Sierra上也遇到了这个问题,当我们如上所述运行pg_ctl时,我们遇到了以下错误。因此,我们按照此处的步骤解决了我们的问题,即:pg_ctl: no database directory specified and environment variable PGDATA unset

mkdir ~/.postgres

initdb ~/.postgres

pg_ctl -D ~/.postgres start

17赞 attaboyabhipro 11/4/2016 #20

解决这个问题最万无一失的方法是做

brew reinstall postgresql

这将使您的用户权限等保持完好无损,并且所有内容都将重置为新的。一直有效!

评论

0赞 Yaseen Ahmad 6/15/2020
这是有效的,但我建议如果在 mac os 上不起作用,请使用此命令。rm /usr/local/var/postgres/postmaster.pid
0赞 user309838 2/23/2023
没错,我尝试删除postmaster.pid,但我不在那里,所以我尝试了这个解决方案,它重新创建了该文件,现在它工作正常。
3赞 Esteban Echeverry 2/9/2017 #21

对于那些使用此命令但不起作用或文件不存在并且正在使用 Ruby on Rails 的用户

rm /usr/local/var/postgres/postmaster.pid

或任何其他命令,然后继续失败。

我用 Brew 解决了这个问题。 我不得不用 brew 卸载 2 次,因为在第一次卸载时会保留另一个版本的 postgresql,第二次卸载该过程将完成。

使用 Brew 安装 postgresql

然后删除、创建和迁移项目的数据库

(别忘了启动postgresql服务器)

69赞 svelandiag 2/9/2017 #22

如果使用 brew 安装和卸载 postgres 不适合您,请查看 postgresql 安装日志或:

postgres -D /usr/local/var/postgres

如果您看到这种输出:

LOG:  skipping missing configuration file "/usr/local/var/postgres/postgresql.auto.conf"
FATAL:  database files are incompatible with server
DETAIL:  The data directory was initialized by PostgreSQL version 9.4, which is not compatible with this version 9.6.1.

然后尝试以下操作:

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

然后启动服务器:

pg_ctl -D /usr/local/var/postgres -l logfile start

评论

5赞 Falk Schuetzenmeister 2/20/2019
当原因与接受的答案不同时,即数据库文件与当前版本不兼容时,这就有效。由于这一直是我的问题,我投了赞成票。
0赞 Theodory 2/24/2022
这个在 mac os big sur 中对我有用。
0赞 svelandiag 10/14/2022
@zepol.j当然需要备份,哈哈
5赞 Cl Local 3/15/2017 #23

如果 postgres 是使用自制软件安装的,则可以通过运行以下命令来解决此问题:

brew link postgres
29赞 rld 3/15/2017 #24
brew services start postgres 

为我工作!

评论

0赞 stingMantis 8/15/2022
这对我有用。出于某种原因,当我更新 brew 时,我收到了 OP 错误。我使用上述命令显式升级了postgres,这似乎有效。
8赞 user1596067 5/6/2017 #25

这实际上是你应该做的:

您应该查看 /usr/local/var/postgres/postmaster.pid

然后查看文件的第一行 - 这是错误的 PID

ps aux | grep <PID>

例如:

ps aux | grep 12345

然后做

kill <PID>

例如

kill 12345

假设它仍在运行

https://superuser.com/questions/553045/fatal-lock-file-postmaster-pid-already-exists

不要听公认的答案,这是不好的,会损坏您的数据!!

评论

0赞 Abe Petrillo 6/23/2017
谢谢,这对我有用。删除已接受答案中的 postmaster.pid 实际上是不够的。
0赞 Thyag 7/9/2017 #26

我使用了 Bitnami 堆栈并以非 root 用户 psql 身份安装。在使用psql时,我确实收到了上述错误。

原来,psql有2个版本

  1. Linux 发行版中预打包的根级 psql。这指向 /usr/bin/psql
  2. Bitnami 堆栈安装的非根级别 psql。这指向 /bitnami/postgresql/postgresql/bin/psql

您可能需要更改 psql 的别名,使其指向非 root 用户。

alias psql='/bitnami/postgresql/postgresql/bin/psql'

以上对我有用psql -U postgres

0赞 Selv 10/10/2017 #27

data 目录包含一个旧的 postmaster.pid 文件/该 data 目录包含一个不可读的 postmaster.pid 文件 PostgreSQL 在数据目录中放置一个名为 postmaster.pid 的文件,用于存储 PostgreSQL 服务器进程的进程 ID。如果 PostgreSQL 崩溃,此文件可能包含混淆 PostgreSQL 的旧 pid。您可以通过删除 postmaster.pid 文件来解决此问题。但是,您必须确保 PostgreSQL 确实没有运行。打开活动监视器,确保没有名为“postgres”或“postmaster”的进程。

如果在 PostgreSQL 运行时删除 postmaster.pid 文件,则会发生不好的事情。

来源:https://postgresapp.com/documentation/troubleshooting.html

4赞 Brilliant-DucN 10/16/2017 #28

我遇到了同样的问题,因为我在代码中使用了错误的 Postgres 用户名。我登录了 postgres 并输入了角色名称并更正了 Postgres 的用户名。psql -d postgres\du

因此,当你们遇到这个问题时,你们需要确保使用正确的 Postgres 用户名、密码、主机名和数据库......

希望这对任何人都有帮助

3赞 Sergio Zapata 10/29/2017 #29

¿您最近是否更改了 pg_hba.conf?如果您只是检查了以下任何拼写错误:

“local”仅适用于 Unix 域套接字连接

本地所有密码

IPv4 本地连接:

主机全部 127.0.0.1/32 密码

IPv6 本地连接:

主机全部 ::1/128 密码

有时,一个简单的错误会让我们头疼。我希望这会有所帮助,如果我的英语一点也不好,对不起。

0赞 Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ 12/4/2017 #30

如果您在升级 Postgres 时(或升级后)遇到问题,请按照此处提到的步骤在版本之间安全地传输数据,这将解决问题:

使用 Homebrew 将 PostgreSQL 9.6.5 升级到 10.0 (macOS)

3赞 Souvik Das 1/4/2018 #31

当 postgres 服务器未运行时,会发生这种情况。 在 MAC 上通过 Homebrew 正确安装 Postgres 的步骤:

  1. brew install postgres

  2. initdb /Users/<username>/db -E utf8[这会初始化 postgres 以使用给定目录作为数据库目录。通常不建议使用用户目录进行数据库存储。编辑 sudoers 文件以添加 initdb 和类似命令,然后在 /usr/local/var/postgres 上运行 initdb]

  3. pg_ctl -D /Users/<username>/db -l logfile start[步骤 2 成功后,将提示运行步骤 3。此命令手动启动服务器。

64赞 Tomer G 1/24/2018 #32

升级数据库对我有用

brew postgresql-upgrade-database

评论

2赞 Rob Bednark 6/3/2020
👍✅⭐️🙏谢谢Tomer!这对我有用 6 年 2 月 20 日在 macOS Mojave 10.14.6 上从 9.4 升级到 12
1赞 Francisco Quintero 8/4/2020
更改 Ruby 版本后,我没有注意到,但我的 Postgresql 安装更改了版本,只有使用此命令才能让它再次运行。
0赞 Sachin 1/18/2021
这也对我有用。2 天前,我更新了导致我的 postgres 的自制软件,但在更新 postgressql 后,它工作正常。再次感谢
0赞 shah 7/20/2021
No such file or directory @ rb_sysopen - /usr/local/var/postgres/PG_VERSIO
0赞 Vikas Pal 12/3/2021
非常感谢!🥳🥳🥳 这对我👍👍👍有用,我认为当我安装其他软件包时,brew 会自动更新很多软件包。
5赞 Ramon Diogo 3/17/2018 #33

这对我有用(作为以前答案的混合):

$ rm /usr/local/var/postgres/postmaster.pid

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

来源:https://coderwall.com/p/zf-fww/postgres-on-osx-with-homebrew-not-running-after-osx-crash

18赞 Abdullah Gürsu 5/20/2018 #34

问题是因为端口 5432 上已经有一个正在运行的服务,我们无法通过这个端口建立 psql 套接字连接。

我删除了套接字文件

rm -rf /tmp/.s.PGSQL.5432/

然后我重新初始化了postgres服务

postgres -D /usr/local/var/postgres

这对我有用。

0赞 Ajay Takur 7/16/2018 #35

原因之一是确保磁盘的大小未满 执行命令postmaster -D postgres的路径

19赞 rld 10/18/2018 #36

该问题也可能归因于导致 postmaster.pid 文件崩溃的进程。 我这样做并工作:

$ brew services stop postgresql
$ rm /usr/local/var/postgres/postmaster.pid # adjust path accordingly to your install
$ brew services start postgresql

评论

0赞 ANimator120 7/29/2022
我在任何地方都找不到这个 .pid 文件
0赞 rld 7/30/2022
尝试 A:如果不尝试 A: 或 A:$ ls -a usr/local/var/postgres$ where postgres$ which postgres
6赞 Piyush Sawaria 4/12/2019 #37

我遇到了同样的问题。psql (PostgreSQL) 9.6.11

什么对我有用 -

删除 postmaster.pid --rm /usr/local/var/[email protected]/postmaster.pid

重新启动 postgres --brew services restart [email protected]

如果这也不起作用,那么运行 --sudo chmod 700 /usr/local/var/[email protected]

1赞 PJSCopeland 5/15/2019 #38

我发现在将最新补丁安装到 Postgres 9.6 后,我所有的可执行文件(、、等)都被丢弃了,我不得不重新链接它们。幸运的是,Homebrew 可以为您做到这一点:postgrespsqlpg_dumppg_restore

brew link --force postgresql # (or in my case, `[email protected]`)
0赞 hooknc 6/14/2019 #39

对于我们来说,我们必须在 psql 命令中定义 localhost,如下所示:

psql -h localhost -U postgres

不知道为什么不默认为...-hlocalhost

评论

0赞 Mark A 6/18/2019
我被拒接
0赞 hooknc 6/18/2019
还行。您的数据库是否在本地主机上运行?
0赞 Mark A 6/18/2019
是的。尝试配置与localhost的基本连接
0赞 hooknc 6/18/2019
您的服务器是否在端口 5432 上运行?
0赞 Mark A 6/18/2019
是的。。我检查了一下,不同的版本是 10 和 11。我不得不跑..brew postgresql-upgrade-database
4赞 Ashwin 7/12/2019 #40

如果您在不退出 psql 的情况下关闭系统,postgres 将不会删除某些文件。

我在usr/local/var/postgres位置没有找到postmaster.pid文件

所以我做了以下事情:

brew services 启动 postgresql

上面的命令应该让你启动postgres

5赞 dimivrn 3/26/2020 #41

所以我在命令之后坚持了这一点。在 macOS Catalina 10.15.3 中设置环境。rails db:create

我检查的第一件事是让我来到这里的流程。在确保所有事情都顺利进行并且没有可能逃脱我脑海中的错误之后,我从这里尝试了最流行的解决方案,但似乎没有一个有效。

到目前为止,我看到的唯一错误是

$ psql psql: 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"?

所以我需要一些关于正在发生的事情的更具体的信息。由于这个原因,我决定查看位于

/usr/local/var/log/postgres.log

所以打开日志后,我看到了这个错误

LOG: starting PostgreSQL 12.2 on x86_64-apple-darwin19.3.0, compiled by Apple clang version 11.0.0 (clang-1100.0.33.17), 64-bit LOG: could not translate host name "localhost", service "5432" to address: nodename nor servname provided, or not known WARNING: could not create listen socket for "localhost" FATAL: could not create any TCP/IP sockets LOG: database system is shut down

所以这更具解释性和具体性。问题在于 PostgreSQL 无法“看到”并解析 localhost 服务器。

所以我做的下一件事是检查 /etc/hosts 文件,其默认内容应如下所示:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1          localhost 

在将上述内容与我的进行比较后,我发现在我的行中是不同的,并评论了(!

#::1             localhost

所以我删除了行前面的 # 符号,保存了文件并重新运行

rails db:create 

数据库已成功启动。

评论

0赞 nayiaw 2/10/2021
上帝保佑你。其他答案对我不起作用,我无法看到 postmaster.pid。我还有其他错误,但感谢上帝在日志文件上提示我进行检查:。./usr/local/var/log/postgres.log
0赞 David Morales 3/26/2020 #42

我必须修复目录的权限:

sudo chmod 0750 /usr/local/var/postgres
1赞 Mehedi Hasan Shifat 4/21/2020 #43

尝试

sudo systemctl start postgresql@12-main

在这里,我的 postgresql 版本是 12。

0赞 Julius Kvedaras 6/27/2020 #44

对我有用的东西我在运行时有 2 个版本的 PostgreSQLbrew services list

Name           Status  User           Plist
consul         stopped                
docker-machine stopped                
mysql          stopped                
postgresql     started homebrew.mxcl.postgresql.plist
[email protected] stopped                
redis          stopped                
runit          stopped                
unbound        stopped                
vault          stopped 

并且刚刚推出了另一个版本brew services start [email protected]

0赞 Anyelo Petit 7/20/2020 #45

就我而言,我正在运行其他项目,我不得不停止 postgresqldocker-composesudo service postgresql stop && docker-compose up -d postgres && docker-compose up rails

为了在我的新项目中修复这个错误,我只需要运行:然后它就修复了它。sudo service postgresql restart

0赞 Esmaeil 9/4/2020 #46

我有 macOS Catalina (10.15.6) 和 PostgreSQL 12。

当我运行时,说它启动PostgreSQL没有问题,但是当我得到时它没有显示。brew services start postgresqlbrewps auxw | grep post

为了解决这个问题,我从这个链接安装,然后安装并按下按钮。Postgres.appInit

它将找到您的数据库并显示给您。

2赞 anna 9/15/2020 #47

有同样的问题。升级数据库成功了!postgresql-upgrade-database

11赞 Hany Moh. 10/29/2020 #48

就我而言,麻烦是由Mac OS更新引起的。升级 PostgreSQL 解决了这个问题。

# upgrade database version solved the trouble
$ brew postgresql-upgrade-database

但是,此错误很常见,并且有多种可能的原因,您不应完全依赖我上面的解决方案。

评论

0赞 Zargold 11/8/2021
请注意,根据您从 brew 和操作系统中获取的 postgresql 的特定版本,任何涉及从特定路径删除文件的解决方案都可能没有正确的路径。就我而言,即使我找到了正确的道路,它仍然失败了——但这奏效了。
0赞 Karl Adler 12/15/2020 #49

MacOS Big Sur 似乎将文件夹重命名为 .因此,更新后的路径将是:Application\ SupportApplicationSupport

/Users/<USERNAME>/Library/ApplicationSupport/Postgres/var-10 
(or "/var-<YOUR PG VERSION>")
2赞 Sajad Rastegar 1/24/2021 #50

我只是重新启动了postgres:

brew services restart postgresql

没有必要删除.postmaster.pid

1赞 Kirandeep Kaur 3/15/2021 #51

我正在MacBook M1(Air)上做一个项目,并打开了一些选项卡,突然我的Postgres服务器停止并开始出现以下错误:

psql:无法连接到服务器:连接失败 服务器是否在本地运行并接受 Unix 域套接字“/tmp/.s.PGSQL.5432”上的连接

我尝试了很多解决方案,例如;

  1. 更新 brew 和 Postgres。
  2. 重新启动服务器。
  3. 试图从命令行终止进程。

但可悲的是,没有什么对我有用。

最后,我想到了 重新启动我的mac .令我惊讶的是,postgres 服务器开始像以前一样正常工作。我认为Postgres目录一定存在一些问题。但我很高兴,它奏效了。

我建议您在重新启动之前先完成上述选项。

谢谢。

10赞 Carlos Gómez 5/28/2021 #52

对于 MacOS BigSur,自制 postgres:

  • rm /opt/homebrew/var/postgres/postmaster.pid
  • brew services restart postgresql
2赞 Thomas Van Holder 7/30/2021 #53

这对我有用:rm /usr/local/var/postgres/postmaster.pid

2赞 Léonard Henriquez 8/30/2021 #54

如果您在 M1 mac 上并且安装了带有 M1 版本自制软件的 postgresql,则此大声工作:

$ rm /opt/homebrew/var/postgres/postmaster.pid
$ brew services restart postgresql
9赞 Suneet Srivastava 9/22/2021 #55

这对我有用.我不必删除任何文件

brew postgresql-upgrade-database

评论

0赞 Mike Williamson 3/26/2022
这也是我的问题;由于 brew 更新了它,我的数据目录的版本比服务器旧。您可以通过查看日志来查看这是否是问题所在: 。如果需要升级,您会看到类似以下内容:postgres -D /usr/local/var/postgresThe data directory was initialized by PostgreSQL version 13, which is not compatible with this version 14.2.
1赞 Suneet Srivastava 10/26/2021 #56

对于 Mac OS Catalina,这有效

$ rm /usr/local/var/postgres/postmaster.pid
$ brew services restart postgresql
0赞 Saeed Hadikhanloo 10/29/2021 #57

这就是它在我的 MacOS 上为我修复此错误的方式(我曾经安装过):homebrewpostgres

cd /opt/homebrew/var
rm -rf postgres
initdb ./postgres -E utf8
pg_ctl -D ./postgres -l logfile start
rm postgres/postmaster.pid
brew services restart postgresql
createdb $(whoami)
1赞 shin 12/7/2021 #58

对于 macOS Monterey,我使用了 .brew services restart postgresql

brew install postgresql
createdb mydb
createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?

brew services restart PostgreSQL
==> Successfully started `postgresql` (label: homebrew.mxcl

0赞 ArunSelvam P M 2/3/2022 #59

对我有用的最快解决方案:

  1. 在为 postgres 配置的端口中查找已在运行的 postgres 进程
lsof -i:5432
  1. 找到 postgres 的进程 ID 并杀死它们
kill -9 <pid>
5赞 Khoi Ngo 5/6/2022 #60

适用于 MacOS M1 Monterey 的解决方案

rm /opt/homebrew/var/postgres/postmaster.pid
brew services restart postgresql
0赞 Piotr Drzymkowski 7/14/2022 #61

我遇到了同样的问题,但解决方案不起作用。 我终于通过更新我的postgres数据库来修复它:

brew postgresql-upgrade-database
0赞 greta 7/25/2022 #62

当我开始课程时,他们提到我需要set_pg,所以每次创建新的终端窗口时我都需要这样做

$set_pg
1赞 Soroush Rasti 9/19/2022 #63

我在 ubuntu 中通过简单地重新启动计算机解决了这个问题。 就我而言,这个问题的原因是 postgres 没有优雅地停止(通过)。sudo kill

0赞 Max Berdnikau 10/14/2022 #64

对于 MacOS Monterey 和 Postgresql@14(路径不同):
rm /usr/local/var/postgresql@14/postmaster.pid
brew services restart postgresql

0赞 Satendra Rawat 10/18/2022 #65

对我来说,使用 brew 升级效果很好。

  • 配置:Mac OS - Monterey M1
brew postgresql-upgrade-database
0赞 Summer Cook 2/4/2023 #66

我几乎尝试了所有这些答案,这就是最终让我开始 postgres 的原因

  • 跟踪 postgres 的日志。
    • 路径需要根据 Postgres 的安装位置和您的版本进行更新。我在 m1 Monterey 上使用 postgresql@14,并用自制软件安装了它。

    • 我终于找到了我需要阅读这篇文章的路径

    • tail /opt/homebrew/var/log/[email protected]

输出显示如下:

2023-02-03 15:33:49.294 CST [82651] FATAL:  could not open directory "pg_notify": No such file or directory
2023-02-03 15:33:49.294 CST [82651] LOG:  database system is shut down
  • 转到 / 目录并cd opt/homebrew/var/postgresql@14

  • 创建缺少的目录(也许这对您来说是一个不同的目录)

    • mkdir pg_notify
  • 对所有缺少的目录重复此过程。

    • 我需要为 、 、 和 的 mkdir 编写 mkdir,但我建议您每次都专门运行 tail 命令,以确保您不会丢失与我不同的目录和文件。pg_tblspcpg_replslotpg_twophasepg_stat_tmppg_logical/snapshotspg_logical/mappingspg_commit_tspg_snapshotspg_commit_ts

最后,在创建每个缺失的目录后反复运行 tail 命令后,我得到了这个输出。

2023-02-03 15:49:18.909 CST [85772] LOG:  redo done at 0/17211D8 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
2023-02-03 15:49:18.914 CST [85771] LOG:  database system is ready to accept connections