提问人:Gary Lai 提问时间:11/16/2012 最后编辑:Nikita FedyashevGary Lai 更新时间:10/11/2023 访问量:597309
Postgres 无法连接到服务器
Postgres could not connect to server
问:
在我做完之后,我的postgres遇到了一些问题。我尝试卸载postgres并重新安装它,但它不起作用。brew update
brew 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
答:
检查套接字文件是否存在。
$ 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/
对于任何阅读本文并使用 Postgres.app 的人,您可能需要在database.yml。http://postgresapp.com/documentation#toc_3host: localhost
评论
原因
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/
更改 postresql 或 database.yml 配置设置、更改$PATH或创建符号链接对我来说都是不必要的。我需要做的就是然后(或)。gem uninstall pg
bundle
gem install pg
问题是 pg gem 是在自制 postgres 之前安装的,因此从 MacOS 附带的 postgres 版本中获取设置。重新安装它(从而重建本机扩展)解决了该问题。
评论
有类似的问题;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
评论
rm: /usr/local/var/postgres/postmaster.pid: No such file or directory
tail /usr/local/var/postgres/server.log
在其他进程访问它们时尝试恢复/删除/创建数据库后,我遇到了这个问题。MacOSX/Homebrew 修复是:
- 关闭所有其他访问进程 , , , , ,
rails server
rails console
guard
- 使用以下命令进行加载/卸载
brew info postgres
- 从之前运行 restore/drop/create
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 根本没有运行(它通常总是在后台运行,但无论出于何种原因,今天都没有)。
如果是这种情况,只需在项目目录的命令行中键入即可postgres
经过大量的来回折腾,它真的归结为我正在使用的宝石版本。在 mavericks 上,版本不会连接到端口 5432,但版本工作正常 - 非常奇怪。pg
pg
0.15.1
0.17.1
在 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
评论
当 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
评论
postgres -D /usr/local/var/postgres
brew services start postgres
postgres -D /usr/local/var/postgres
安装后只需两个步骤即可运行数据库(在此之前,请确保您的登录身份)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
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"?
在这里找到了适合我的解决方案:
基本上,您可以运行以下命令来手动启动服务器:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
评论
看起来你的psql没有运行。您应该在连接之前运行它。您只能使用 Postgres.app for Mac OS 执行此操作。(下载并安装此应用程序 http://postgresapp.com)打开应用程序,您已经准备好了 PostgreSQL 服务器并等待新连接。关闭应用程序,服务器将关闭。您也可以在此处找到此信息 http://www.postgresql.org/download/macosx/。希望这对你有所帮助。
评论
当我从 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
评论
这是因为以前的服务器仍在运行,请尝试关闭所有内容并重新运行您的应用程序。
对我来说,是apache升级导致了问题。我仍然可以在控制台中运行 psql 或直接从 kdevelop 调用 db。此外,它还可以将“host=localhost”添加到连接字符串中。
但真正的问题是 apache 已经更改为私有 tmp。
溶液:更新 /usr/lib/systemd/system/apache2.service 并将 PrivateTmp=true 更改为 PrivateTmp=false。
我正在开发 OpenSuse OS,但我想在 Mac 上可能会发生类似的事情。
在MacOS Sierra上也遇到了这个问题,当我们如上所述运行pg_ctl时,我们遇到了以下错误。因此,我们按照此处的步骤解决了我们的问题,即:pg_ctl: no database directory specified and environment variable PGDATA unset
mkdir ~/.postgres
initdb ~/.postgres
pg_ctl -D ~/.postgres start
解决这个问题最万无一失的方法是做
brew reinstall postgresql
这将使您的用户权限等保持完好无损,并且所有内容都将重置为新的。一直有效!
评论
rm /usr/local/var/postgres/postmaster.pid
对于那些使用此命令但不起作用或文件不存在并且正在使用 Ruby on Rails 的用户
rm /usr/local/var/postgres/postmaster.pid
或任何其他命令,然后继续失败。
我用 Brew 解决了这个问题。 我不得不用 brew 卸载 2 次,因为在第一次卸载时会保留另一个版本的 postgresql,第二次卸载该过程将完成。
使用 Brew 安装 postgresql
然后删除、创建和迁移项目的数据库
(别忘了启动postgresql服务器)
如果使用 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
评论
如果 postgres 是使用自制软件安装的,则可以通过运行以下命令来解决此问题:
brew link postgres
brew services start postgres
为我工作!
评论
这实际上是你应该做的:
您应该查看 /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
不要听公认的答案,这是不好的,会损坏您的数据!!
评论
我使用了 Bitnami 堆栈并以非 root 用户 psql 身份安装。在使用psql时,我确实收到了上述错误。
原来,psql有2个版本
- Linux 发行版中预打包的根级 psql。这指向 /usr/bin/psql
- Bitnami 堆栈安装的非根级别 psql。这指向 /bitnami/postgresql/postgresql/bin/psql
您可能需要更改 psql 的别名,使其指向非 root 用户。
alias psql='/bitnami/postgresql/postgresql/bin/psql'
以上对我有用psql -U postgres
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
我遇到了同样的问题,因为我在代码中使用了错误的 Postgres 用户名。我登录了 postgres 并输入了角色名称并更正了 Postgres 的用户名。psql -d postgres
\du
因此,当你们遇到这个问题时,你们需要确保使用正确的 Postgres 用户名、密码、主机名和数据库......
希望这对任何人都有帮助
¿您最近是否更改了 pg_hba.conf?如果您只是检查了以下任何拼写错误:
“local”仅适用于 Unix 域套接字连接
本地所有密码
IPv4 本地连接:
主机全部 127.0.0.1/32 密码
IPv6 本地连接:
主机全部 ::1/128 密码
有时,一个简单的错误会让我们头疼。我希望这会有所帮助,如果我的英语一点也不好,对不起。
如果您在升级 Postgres 时(或升级后)遇到问题,请按照此处提到的步骤在版本之间安全地传输数据,这将解决问题:
使用 Homebrew 将 PostgreSQL 9.6.5 升级到 10.0 (macOS)
当 postgres 服务器未运行时,会发生这种情况。 在 MAC 上通过 Homebrew 正确安装 Postgres 的步骤:
brew install postgres
initdb /Users/<username>/db -E utf8
[这会初始化 postgres 以使用给定目录作为数据库目录。通常不建议使用用户目录进行数据库存储。编辑 sudoers 文件以添加 initdb 和类似命令,然后在 /usr/local/var/postgres 上运行 initdb]pg_ctl -D /Users/<username>/db -l logfile start
[步骤 2 成功后,将提示运行步骤 3。此命令手动启动服务器。
升级数据库对我有用
brew postgresql-upgrade-database
评论
No such file or directory @ rb_sysopen - /usr/local/var/postgres/PG_VERSIO
这对我有用(作为以前答案的混合):
$ 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
问题是因为端口 5432 上已经有一个正在运行的服务,我们无法通过这个端口建立 psql 套接字连接。
我删除了套接字文件
rm -rf /tmp/.s.PGSQL.5432/
然后我重新初始化了postgres服务
postgres -D /usr/local/var/postgres
这对我有用。
原因之一是确保磁盘的大小未满 执行命令postmaster -D 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 -a usr/local/var/postgres
$ where postgres
$ which postgres
我遇到了同样的问题。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]
我发现在将最新补丁安装到 Postgres 9.6 后,我所有的可执行文件(、、等)都被丢弃了,我不得不重新链接它们。幸运的是,Homebrew 可以为您做到这一点:postgres
psql
pg_dump
pg_restore
brew link --force postgresql # (or in my case, `[email protected]`)
对于我们来说,我们必须在 psql 命令中定义 localhost,如下所示:
psql -h localhost -U postgres
不知道为什么不默认为...-h
localhost
评论
brew postgresql-upgrade-database
如果您在不退出 psql 的情况下关闭系统,postgres 将不会删除某些文件。
我在usr/local/var/postgres位置没有找到postmaster.pid文件
所以我做了以下事情:
brew services 启动 postgresql
上面的命令应该让你启动postgres
所以我在命令之后坚持了这一点。在 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
数据库已成功启动。
评论
/usr/local/var/log/postgres.log
我必须修复目录的权限:
sudo chmod 0750 /usr/local/var/postgres
尝试
sudo systemctl start postgresql@12-main
在这里,我的 postgresql 版本是 12。
对我有用的东西我在运行时有 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]
就我而言,我正在运行其他项目,我不得不停止 postgresqldocker-compose
sudo service postgresql stop && docker-compose up -d postgres && docker-compose up rails
为了在我的新项目中修复这个错误,我只需要运行:然后它就修复了它。sudo service postgresql restart
我有 macOS Catalina (10.15.6) 和 PostgreSQL 12。
当我运行时,说它启动PostgreSQL没有问题,但是当我得到时它没有显示。brew services start postgresql
brew
ps auxw | grep post
为了解决这个问题,我从这个链接安装,然后安装并按下按钮。Postgres.app
Init
它将找到您的数据库并显示给您。
有同样的问题。升级数据库成功了!postgresql-upgrade-database
就我而言,麻烦是由Mac OS更新引起的。升级 PostgreSQL 解决了这个问题。
# upgrade database version solved the trouble
$ brew postgresql-upgrade-database
但是,此错误很常见,并且有多种可能的原因,您不应完全依赖我上面的解决方案。
评论
MacOS Big Sur 似乎将文件夹重命名为 .因此,更新后的路径将是:Application\ Support
ApplicationSupport
/Users/<USERNAME>/Library/ApplicationSupport/Postgres/var-10
(or "/var-<YOUR PG VERSION>")
我只是重新启动了postgres:
brew services restart postgresql
没有必要删除.postmaster.pid
我正在MacBook M1(Air)上做一个项目,并打开了一些选项卡,突然我的Postgres服务器停止并开始出现以下错误:
psql:无法连接到服务器:连接失败 服务器是否在本地运行并接受 Unix 域套接字“/tmp/.s.PGSQL.5432”上的连接
我尝试了很多解决方案,例如;
- 更新 brew 和 Postgres。
- 重新启动服务器。
- 试图从命令行终止进程。
但可悲的是,没有什么对我有用。
最后,我想到了 重新启动我的mac .令我惊讶的是,postgres 服务器开始像以前一样正常工作。我认为Postgres目录一定存在一些问题。但我很高兴,它奏效了。
我建议您在重新启动之前先完成上述选项。
谢谢。
对于 MacOS BigSur,自制 postgres:
rm /opt/homebrew/var/postgres/postmaster.pid
brew services restart postgresql
这对我有用:rm /usr/local/var/postgres/postmaster.pid
如果您在 M1 mac 上并且安装了带有 M1 版本自制软件的 postgresql,则此大声工作:
$ rm /opt/homebrew/var/postgres/postmaster.pid
$ brew services restart 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.
对于 Mac OS Catalina,这有效
$ rm /usr/local/var/postgres/postmaster.pid
$ brew services restart postgresql
这就是它在我的 MacOS 上为我修复此错误的方式(我曾经安装过):homebrew
postgres
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)
对于 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
对我有用的最快解决方案:
- 在为 postgres 配置的端口中查找已在运行的 postgres 进程
lsof -i:5432
- 找到 postgres 的进程 ID 并杀死它们
kill -9 <pid>
适用于 MacOS M1 Monterey 的解决方案
rm /opt/homebrew/var/postgres/postmaster.pid
brew services restart postgresql
我遇到了同样的问题,但解决方案不起作用。 我终于通过更新我的postgres数据库来修复它:
brew postgresql-upgrade-database
当我开始课程时,他们提到我需要set_pg,所以每次创建新的终端窗口时我都需要这样做
$set_pg
我在 ubuntu 中通过简单地重新启动计算机解决了这个问题。
就我而言,这个问题的原因是 postgres 没有优雅地停止(通过)。sudo kill
对于 MacOS Monterey 和 Postgresql@14(路径不同):
rm /usr/local/var/postgresql@14/postmaster.pid
brew services restart postgresql
对我来说,使用 brew 升级效果很好。
- 配置:Mac OS - Monterey M1
brew postgresql-upgrade-database
我几乎尝试了所有这些答案,这就是最终让我开始 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_tblspc
pg_replslot
pg_twophase
pg_stat_tmp
pg_logical/snapshots
pg_logical/mappings
pg_commit_ts
pg_snapshots
pg_commit_ts
- 我需要为 、 、 和 的 mkdir 编写 mkdir,但我建议您每次都专门运行 tail 命令,以确保您不会丢失与我不同的目录和文件。
最后,在创建每个缺失的目录后反复运行 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
评论
brew postgresql-upgrade-database