在我做了 brew update
和 brew upgrade
之后,我的 postgres 出现了一些问题。我尝试卸载 postgres 并重新安装,但效果不佳。
这是错误信息。 (当我尝试执行 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 正在卸载 /usr/local/Cellar/postgresql/9.2.1... $ brew uninstall postgresql 正在卸载 /usr/local/Cellar/postgresql/9.1.4... $ psql --version bash: /usr/ local/bin/psql:没有这样的文件或目录 $ brew install postgresql ==> 正在下载 http://ftp.postgresql.org/pub/source/v9.2.1/postgresql-9.2.1.tar.bz2 已经下载:/ Library/Caches/Homebrew/postgresql-9.2.1.tar.bz2 ...... ...... ==> 摘要 /usr/local/Cellar/postgresql/9.2.1:2814 个文件,38M,已构建2.7 分钟后 $ initdb /usr/local/var/postgres -E utf8 属于该数据库系统的文件将归用户“laigary”所有。该用户还必须拥有服务器进程。数据库集群将使用语言环境“en_US.UTF-8”进行初始化。默认文本搜索配置将设置为“英语”。 initdb: 目录 "/usr/local/var/postgres" 存在但不为空 如果要创建新的数据库系统,请删除或清空目录 "/usr/local/var/postgres" 或使用参数运行 initdb除了“/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: 已经加载 $ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log 启动服务器 $ env ARCHFLAGS="-arch x86_64 " gem install pg 构建原生扩展。这可能需要一段时间... 成功安装 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 服务器是否在本地运行并接受 Unix 域套接字“/tmp/.s.PGSQL.5432”上的连接?
现在,在我重新安装自制软件后,当我使用 $ psql
时,它不会显示任何错误消息。
但我在我的 Rails 应用程序中运行 rake db:migrate
,它显示:
无法连接到服务器:没有这样的文件或目录服务器是否在本地运行并接受 Unix 域套接字“/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/gem s/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:在“连接”/ 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 `连接' /usr/l ocal/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 <顶部(必需)>' /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:在“每个”/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
最后我找到了解决方案。
sudo mkdir /var/pgsql_socket/
sudo ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/
这个解决方案有点棘手,但它确实有效。希望有人有更好的解决方案
更新
这也适用于我。
rm /usr/local/var/postgres/postmaster.pid
brew postgresql-upgrade-database
有类似的问题; 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
当 brew 进行 postgres 升级时,有时会发生这种情况,导致数据文件与新服务器不兼容。
就我而言,它发生在 upgrading from 9.3 to 9.4 时。
OS X/自制软件:
尝试运行 postgres -D /usr/local/var/postgres
- 如果 postgres 无法启动,它将为您提供更详细的输出。或在 /usr/local/var/log/postgres.log
(Mac M1 上为 /opt/homebrew/var/log/postgres.log
)打开日志文件并查找行 “FATAL: database files are incompatible with server”
在我的例子中,运行 rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8
删除了我的旧数据库,然后重新初始化了 postgres db 模式。 (这会破坏您的数据)
感谢 https://github.com/Homebrew/homebrew/issues/35240 提供的解决方案。可以在此处找到彻底重新安装的完整说明:How to completely uninstall and reinstall Homebrew Postgres - Test Double Blog(同样,如果您使用的是 M1 Mac,则将 /opt/homebrew/var 路径替换为 /usr/local/var)
重新生成我的数据库(使用 rake db:create
)后,一切都恢复正常了。
最后,评论中的链接指向保留您的数据的可能解决方案,但我没有尝试过:How to upgrade PostgreSQL from version 9.6 to version 10.1 without losing data - Stack Overflow
postgres -D /usr/local/var/postgres
告诉您数据目录是使用旧的、不兼容的 Postgres 版本初始化的,并且您不想希望丢失本地数据,则可以尝试 this thread
brew services start postgres
而不是 postgres -D /usr/local/var/postgres
在后台启动 postgres
在这里找到了对我有用的解决方案:
您基本上运行以下命令来手动启动服务器:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
如果使用 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
升级数据库对我有用
brew postgresql-upgrade-database
No such file or directory @ rb_sysopen - /usr/local/var/postgres/PG_VERSIO
在 Yosemite 上,如果 pid 文件阻止 Postgres 启动并且您有一个 launchctl
守护程序尝试(并且失败)加载数据库守护程序,那么您需要卸载 plist 文件:
$ 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
对于阅读本文并使用 Postgres.app 的任何人,您的 database.yml 中可能需要 host: localhost
。 http://postgresapp.com/documentation#toc_3
brew services start postgres
为我工作!
该问题也可归因于导致 postmaster.pid 文件落后的崩溃进程。我这样做并工作:
$ brew services stop postgresql
$ rm /usr/local/var/postgres/postmaster.pid # adjust path accordingly to your install
$ brew services start postgresql
检查套接字文件是否存在。
$ 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
$ 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
$ mkdir /var/pgsql_socket/
$ sudo mkdir /var/pgsql_socket/
$ ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/
这个解决方案有点棘手,但很有效。希望有人有更好的解决方案
问题是因为 5432 端口上已经有一个正在运行的服务,我们无法通过这个端口建立 psql 套接字连接。
我删除了套接字文件
rm -rf /tmp/.s.PGSQL.5432/
然后我重新初始化了 postgres 服务
postgres -D /usr/local/var/postgres
这对我有用。
最简单的方法是这样做
brew reinstall postgresql
这将使您的用户权限等保持不变,一切都将重置为新的。一直有效!
rm /usr/local/var/postgres/postmaster.pid
不起作用,则使用此命令。
就我而言,问题是由 Mac OS 更新引起的。升级 PostgreSQL 解决了这个问题。
# upgrade database version solved the trouble
$ brew postgresql-upgrade-database
但是,这个错误很常见并且有多种可能的原因,你不应该完全依赖我上面的解决方案。
这实际上是你应该做的:
你应该看看 /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
不要听公认的答案,这是不好的,会破坏你的数据!!!
对于 MacOS BigSur,自制 postgres:
rm /opt/homebrew/var/postgres/postmaster.pid
brew 服务重启 postgresql
这对我有用。我不必删除任何文件
brew postgresql-upgrade-database
postgres -D /usr/local/var/postgres
。如果您需要升级,您会看到类似 The data directory was initialized by PostgreSQL version 13, which is not compatible with this version 14.2.
更改 postresql 或 database.yml 配置设置、更改 $PATH 或创建符号链接对我来说都是不必要的。我需要做的只是 gem uninstall pg
,然后是 bundle
(或 gem install pg
)。
问题是 pg gem 是在自制 postgres 之前安装的,所以从 MacOS 附带的 postgres 版本中获取设置。重新安装它(从而重建本机扩展)解决了这个问题。
当我从 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 uninstall pg
$ gem uninstall activerecord-postgresql-adapter
$ bundle install
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
无需做软链接
如果 postgres 是使用 homebrew 安装的,你可以通过运行来解决这个问题:
brew link postgres
这对我有用(作为以前答案的混合):
$ 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
psql (PostgreSQL) 9.6.11
我遇到了同样的问题。
什么对我有用-
删除 postmaster.pid -- rm /usr/local/var/postgresql@9.6/postmaster.pid
重新启动 postgres -- brew services restart postgresql@9.6
如果这也不起作用,请运行 -- sudo chmod 700 /usr/local/var/postgresql@9.6
在 MacOS Sierra 上也遇到了这个问题,当我们如上所述运行 pg_ctl 时,我们遇到了以下错误 pg_ctl: no database directory specified and environment variable PGDATA unset
。所以我们按照解决我们问题的步骤here,即:
mkdir ~/.postgres
initdb ~/.postgres
pg_ctl -D ~/.postgres start
我遇到了同样的问题,因为我在代码中使用了错误的 Postgres 用户名。我登录到 postgres psql -d postgres
并输入 \du
以获取角色名称并更正 Postgres 的用户名。
所以当你们遇到这个问题时,你们需要确保使用正确的 Postgres 用户名、密码、主机名和数据库......
希望这会帮助任何人
如果你在不退出 psql 的情况下关闭系统,postgres 就不会删除一些文件。
我没有在 usr/local/var/postgres 位置找到文件 postmaster.pid
所以我做了以下事情:
brew 服务启动 postgresql
上面的命令应该让你启动 postgres
所以我在 rails db:create
命令之后偶然发现了这个。在 macOS Catalina 10.15.3 中设置环境。
我检查的第一件事是让我来到这里的流程。在确保一切顺利并且没有任何错误可能逃过我的脑海后,我尝试了这里最流行的解决方案,但似乎都没有奏效。
到目前为止,我看到的唯一错误是
$ 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"?
所以我需要一些关于正在发生的事情的更具体的信息。由于这个原因,我决定查看位于的 postgres 日志文件
/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
并且数据库已成功启动。
/usr/local/var/log/postgres.log
。
在其他进程正在访问它们时尝试恢复/删除/创建数据库后,我遇到了这个问题。 MacOSX/Homebrew 修复是:
关闭所有其他访问进程 rails server、rails console、guard 等... 使用 brew info postgres run restore/drop/create from before 中的命令加载/卸载
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"?
看起来你的 psql 没有运行。您应该在连接之前运行它。您只能使用适用于 Mac OS 的 Postgres.app 来做到这一点。 (下载并安装此应用程序 http://postgresapp.com)打开应用程序,您已准备好 PostgreSQL 服务器并等待新的连接。关闭应用程序,服务器关闭。您还可以在此处找到此信息 http://www.postgresql.org/download/macosx/。希望这会帮助你。
这是因为以前的服务器仍在运行,请尝试关闭所有内容并重新运行您的应用程序。
不定期副业成功案例分享
rm: /usr/local/var/postgres/postmaster.pid: No such file or directory
tail /usr/local/var/postgres/server.log