首页 文章

Postgres无法连接到服务器

提问于
浏览
318

在我做了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}

Finally I find the solution.

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

这个解决方案有点棘手,但它确实有效 . 希望有人有更好的解决方案

Update

这对我也有用 .

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

30 回答

  • 58

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

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

    然后一切都很好 .

  • 652

    这有时可能是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

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

    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
    
  • 14

    在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
    
  • 207

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

  • 2

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

  • 3
    brew services start postgres
    

    为我工作!

  • 3

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

    $ 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
    
  • 2

    升级数据库对我有用

    brew postgresql-upgrade-database

  • 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
    

    更新的Gems(用于rails / active记录):

    $ gem uninstall pg
     $ gem uninstall activerecord-postgresql-adapter
     $ bundle install
    
  • 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

    do not listen to the accepted answer it is bad and will corrupt your data!!!

  • 7

    这个问题也可归因于一个崩溃的进程,后面留下了postmaster.pid文件 . 我这样做并且工作:

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

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

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

  • 2

    Psql选项

    -h hostname --host = hostname

    :指定运行服务器的计算机的主机名 . If the value begins with a slash, it is used as the directory for the Unix-domain socket.

    $ 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
    

    无需 Build 软链接

  • 4

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

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

  • 25

    在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

  • 3

    最简单的方法是做到这一点

    brew reinstall postgresql
    

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

  • 8

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

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

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

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

    使用Brew安装postgresql

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

    (别忘了启动postgresql服务器)

  • 6

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

    brew link postgres
    
  • 7

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

    $ 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

  • 3

    上述解决方案均不适合我 .

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

    我删除了套接字文件

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

    然后我重新初始化postgres服务

    postgres -D /usr/local/var/postgres
    

    这对我有用 .

  • 3

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

    • 关闭所有其他访问进程 rails serverrails consoleguard 等...

    • 使用 brew info postgres 中的命令加载/卸载

    • 从之前运行restore / drop / create

  • 30

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

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

  • 3

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

  • 27

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

  • 3

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

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

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

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

  • 2

    我得到了相同的问题,因为我在代码中使用了'm using a wrong Postgres'的用户名 . 我登录postgres psql -d postgres 并输入 \du 以获取角色名称并更正Postgres的用户名 .

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

    希望这对任何人都有帮助

  • 2

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

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

    本地所有密码

    IPv4本地连接:

    托管所有127.0.0.1/32密码

    IPv6本地连接:

    托管所有:: 1/128密码

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

  • 2

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

相关问题