Postgres无法连接到服务器

时间:2012-11-16 04:53:40

标签: ruby-on-rails database postgresql macos homebrew

在我做了brew更新和brew升级后,我的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

这就是我所做的。

12:30 ~/D/works$ brew uninstall postgresql
Uninstalling /usr/local/Cellar/postgresql/9.2.1...
12:31 ~/D/works$ brew uninstall postgresql
Uninstalling /usr/local/Cellar/postgresql/9.1.4...
12:31 ~/D/works$ psql --version
bash: /usr/local/bin/psql: No such file or directory
12:33 ~/D/works$ 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
12:37 ~/D/works$ 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".
12:39 ~/D/works$ mkdir -p ~/Library/LaunchAgents
12:39 ~/D/works$   cp /usr/local/Cellar/postgresql/9.2.1/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
12:39 ~/D/works$   launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
homebrew.mxcl.postgresql: Already loaded
12:39 ~/D/works$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
server starting
12:39 ~/D/works$ 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
12:42 ~/D/works$ psql --version
psql (PostgreSQL) 9.2.1
12:42 ~/D/works$ 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

53 个答案:

答案 0 :(得分:711)

有类似的问题;一个pid文件阻止postgres启动。解决它:

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

然后一切都很顺利。

答案 1 :(得分:225)

这有时可能是postgres升级的问题。

在我的情况下,它发生在从9.3升级到9.4。

请参阅http://www.postgresql.org/docs/9.4/static/upgrading.html

OS X / Homebrew:

尝试运行postgres -D /usr/local/var/postgres - 如果postgres无法启动,它将为您提供更详细的输出。

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

感谢https://github.com/Homebrew/homebrew/issues/35240获得该解决方案。

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

答案 2 :(得分:65)

找到一个适合我的解决方案:

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

答案 3 :(得分:32)

如果使用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

Source

答案 4 :(得分:31)

在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

答案 5 :(得分:28)

对于阅读此内容并使用Postgres.app的任何人,您可能需要在database.yml中使用host: localhosthttp://postgresapp.com/documentation#toc_3

答案 6 :(得分:17)

{{1}}

为我工作!

答案 7 :(得分:16)

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

$ 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

答案 8 :(得分:16)

Upgrading the database works for me

brew postgresql-upgrade-database

答案 9 :(得分:12)

该问题还可以归因于崩溃的进程,该进程使postmaster.pid文件滞后。 我这样做是工作的:

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

答案 10 :(得分:7)

这实际上是你应该做的:

你应该看一下/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

不要听取已接受的答案,这样做会损坏您的数据!

答案 11 :(得分:7)

当我从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

更新了Gems(用于rails /活动记录):

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

答案 12 :(得分:6)

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

问题是pg gem已经在自制的postgres之前安装了,所以从MacOS附带的postgres版本中获取了设置。重新安装它(从而重建原生扩展)解决了这个问题。

答案 13 :(得分:6)

问题是因为端口5432上已有运行服务,我们无法通过此端口建立psql套接字连接。

我删除了套接字文件

LANGUAGE

然后我重新初始化postgres服务

 SaveFileDialog sf = new SaveFileDialog();
 sf.Filter = "Json files (*.json)|*.json";
 sf.FilterIndex = 2;
 sf.RestoreDirectory = true;
 if (sf.ShowDialog() == DialogResult.OK)
 {                  
     System.IO.File.Copy(@"D:\somefile.json", sf.FileName, true);   
 }

这对我有用。

答案 14 :(得分:6)

最简单的方法是做

brew reinstall postgresql

这将保持您的用户权限等完整,所有内容都重置为新。一直有效!

答案 15 :(得分:4)

Psql选项

-h hostname 主机=主机名

:指定运行服务器的计算机的主机名。 如果值以斜杠开头,则将其用作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

无需建立软链接

答案 16 :(得分:4)

如果使用自制软件安装postgres,您可以通过运行:

来解决此问题
brew link postgres

答案 17 :(得分:4)

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

$ 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 :(得分:3)

¿你最近改变了pg_hba.conf吗?如果您确实检查过任何拼写错误:

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

本地所有密码

IPv4本地连接:

托管所有127.0.0.1/32密码

IPv6本地连接:

主持所有:: 1/128密码

有时一个简单的错误会让我们头疼。我希望这有帮助,如果我的英语一点都不好,我很抱歉。

答案 19 :(得分:3)

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

  

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

或任何其他命令,只是继续失败。

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

使用Brew安装postgresql

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

(别忘了启动postgresql服务器)

答案 20 :(得分:3)

在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

答案 21 :(得分:3)

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

答案 22 :(得分:3)

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

答案 23 :(得分:3)

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"?

答案 24 :(得分:2)

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

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

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

答案 25 :(得分:2)

当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.此命令将手动启动服务器。]

答案 26 :(得分:2)

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

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

即可

答案 27 :(得分:2)

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

所以,当你们面对这个问题时,你们需要确保使用正确的Postgres用户名,密码,主机名和数据库......

希望这会对任何人有所帮助

答案 28 :(得分:2)

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

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

解决方案:更新/usr/lib/systemd/system/apache2.service并将PrivateTmp = true更改为PrivateTmp = false。

我正在开发OpenSuse操作系统,但我想在Mac上会发生类似的情况。

答案 29 :(得分:2)

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

  1. 关闭所有其他访问流程rails serverrails consoleguard等...
  2. 使用brew info postgres
  3. 中的命令加载/卸载
  4. 从之前运行restore / drop / create

答案 30 :(得分:2)

经过大量的来回反复,它真正归结为我正在使用的pg宝石版本。在小牛身上,pg版本0.15.1无法连接到端口5432,但版本0.17.1工作正常 - 非常奇怪。

答案 31 :(得分:1)

我只是重新启动了 postgres:

brew services restart postgresql

无需删除 postmaster.pid

答案 32 :(得分:1)

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

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

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

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

但遗憾的是,对我来说没有任何效果。

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

建议您在重新启动之前先通过上述选项。

谢谢。

答案 33 :(得分:1)

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

对我有用的东西-

删除postmaster.pid-rm /usr/local/var/postgresql@9.6/postmaster.pid

重新启动postgres-brew services restart postgresql@9.6

答案 34 :(得分:1)

因此,我在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 

数据库成功启动。

答案 35 :(得分:1)

尝试

sudo systemctl start postgresql@12-main

这里我的postgresql版本是12。

答案 36 :(得分:0)

MacOS Big Sur 似乎已将Application\ Support文件夹重命名为ApplicationSupport。因此更新后的路径为:

/Users/<USERNAME>/Library/ApplicationSupport/Postgres/var-10 
(or "/var-<YOUR PG VERSION>")

答案 37 :(得分:0)

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

答案 38 :(得分:0)

对于 MacOS BigSur,自制 postgres:

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

答案 39 :(得分:0)

有同样的问题。升级数据库就可以了! postgresql-upgrade-database

答案 40 :(得分:0)

我有macOS Catalina(10.15.6)和PostgreSQL 12。

当我运行brew services start postgresql时,brew说它可以毫无问题地启动PostgreSQL,但是当我得到ps auxw | grep post时它不会显示。

要解决此问题,我从this link安装了Postgres.app,然后安装并按Init按钮。

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

答案 41 :(得分:0)

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

要在我的新项目中修复此错误,我只需要运行:sudo service postgresql restart,然后就可以对其进行修复。

答案 42 :(得分:0)

对我有用的是我在运行brew services list时有2个版本的PostgreSQL

Name           Status  User           Plist
consul         stopped                
docker-machine stopped                
mysql          stopped                
postgresql     started homebrew.mxcl.postgresql.plist
postgresql@9.6 stopped                
redis          stopped                
runit          stopped                
unbound        stopped                
vault          stopped 

并刚刚启动了另一个版本brew services start postgresql@9.6

答案 43 :(得分:0)

我必须修复目录的权限:

sudo chmod 0750 /usr/local/var/postgres

答案 44 :(得分:0)

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

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

所以我做了以下事情:

  

酿酒服务启动PostgreSQL

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

答案 45 :(得分:0)

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

psql -h localhost -U postgres

不确定为什么-h不默认为localhost ...

答案 46 :(得分:0)

我发现在为Postgres 9.6安装最新的补丁程序之后,我所有的可执行文件(postgrespsqlpg_dumppg_restore等)都被删除了,我不得不重新链接它们。幸运的是,自制啤酒可以为您做到这一点:

brew link --force postgresql # (or in my case, `postgresql@9.6`)

答案 47 :(得分:0)

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

答案 48 :(得分:0)

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

Upgrade PostgreSQL 9.6.5 to 10.0 using Homebrew (macOS)

答案 49 :(得分:0)

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

如果在PostgreSQL运行时删除postmaster.pid文件,则会发生错误。

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

答案 50 :(得分:0)

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

原来,有两个版本的 psql

  1. 在linux发行版中预先打包的根级psql。这指向/ usr / bin / psql
  2. Bitnami堆栈安装的非根级别psql。这指向/ bitnami / postgresql / postgresql / bin / psql
  3. 您可能想要更改psql的别名,以便它指向非root用户。

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

    上面的内容对我来说是psql -U postgres

答案 51 :(得分:0)

安装后运行数据库只需两步(之前确保您已记录为postgres用户)

  

已安装-Dirs / bin / postmaster -D已安装-Dirs / pgsql / data

举个例子:

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

步骤2:从已安装的路径运行psql(检查您安装的位置&#39; #which postgres&#39;将用于查找已安装的位置)

[postgres@localhost bin]$ psql 

答案 52 :(得分:0)

原因

Lion附带了已安装的postgres版本,默认使用这些二进制文件。一般来说,你可以通过使用自制postgres二进制文件的完整路径来解决这个问题,但是其他程序可能仍有问题。

解决方案

curl http://nextmarvel.net/blog/downloads/fixBrewLionPostgres.sh | sh

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