Postgres无法连接到服务器

我做了更新和酿造升级后,我的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"? 

现在,我重新安装了HowBrew后,当我使用$ 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} 

最后我find解决办法。

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

这个解决scheme是有点棘手,但它的工作原理。 希望任何人有更好的解决scheme

更新

这也适用于我。

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

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

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

然后一切都很好。

这有时可能是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解决scheme。;

重新生成我的数据库后(与rake db:create )一切正常工作再次。

find了一个解决scheme,在我这里工作:

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 

对于读这个和使用Postgres.app的人来说,你可能需要在你的database.yml中使用host: localhosthttp://postgresapp.com/documentation#toc_3

在优胜美地,如果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 

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

资源

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

 $ 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 
 brew services start 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 

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

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

这实际上是你应该做的:

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

不要听取接受的答案,这是不好的,会破坏你的数据!

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 

没有必要做一个软链接

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

问题是pg gem已经安装在自制程序postgres之前,所以从MacOS附带的postgres版本中select了设置。 重新安装(并因此重build本机扩展)解决了这个问题。

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

  1. closures所有其他的访问进程rails serverrails consoleguard等…
  2. 使用brew info postgres的命令加载/卸载
  3. 从之前运行恢复/删除/创build

我得到了同样的错误。 事实certificate,postgres根本没有运行(它通常总是在后台运行,但无论什么原因,它不是今天)。

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

FWIW今天发生在我身上,但是当时我正在运行Ubuntu更新,这可能会更新Postgres。 一旦更新完成,我就可以顺利连接。

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

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

这个最愚蠢的办法就是这样做

 brew reinstall postgresql 

这将保持您的用户权限等完整无缺,一切都重置为新的。 一直工作!

对于那些使用这个命令并且不起作用或者文件不在那里并且正在使用Ruby on Rails的人

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

或者任何其他的命令,只是不断失败。

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

用Brew安装postgresql

然后删除,创build和移植项目的数据库

(不要忘记启动postgresql服务器)

如果postgres是使用自制软件安装的,可以运行以下命令修复:

 brew link postgres 

我得到同样的问题,因为我在代码中使用错误的Postgres的用户名。 我login到postgres psql -d postgres并input\du作为angular色名称和正确的Postgres的用户名。

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

希望这会帮助任何人

你最近是否改变了pg_hba.conf? 如果你只是检查在任何错字:

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

本地所有的所有密码

IPv4本地连接:

主机全部全部为127.0.0.1/32的密码

IPv6本地连接:

主持所有:: 1/128密码

有时候一个简单的错误会让我们头疼。 如果我的英文不好,我希望能够帮助和抱歉。

原因

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

解决scheme

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

通过

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

经过大量的来回之后,真正归结为我使用的pg gem版本。 在0.15.1pg版本0.15.1不能连接到5432端口,但是0.17.1版本工作得很好 – 很奇怪。

它看起来像你的psql不运行。 连接之前应该运行它。 你只能使用Postgres.app for Mac OS来做到这一点。 (下载并安装此应用程序http://postgresapp.com )打开应用程序,准备好PostgreSQL服务器并等待新的连接。 closures应用程序,服务器closures。 你也可以在这里find这个信息http://www.postgresql.org/download/macosx/ 。 希望这会帮助你。

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

对我来说,这是一个导致问题的Apache升级。 我仍然可以在控制台中运行psql,或者直接从kdevelop调用db。 此外它工作添加“主机=本地主机”连接string。

但真正的问题是,Apache已经改变为私人tmp。

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

我正在使用OpenSuse操作系统,但我猜想在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

我使用了Bitnami堆栈并以非root用户psql的forms安装。 关于使用psql我确实收到提到的错误。

原来,有2个版本的psql

  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

数据目录包含一个旧的postmaster.pid文件/ data目录包含一个不可读的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

只需两个步骤即可在安装之后运行数据库(在此之前,请确保您的日志logging为postgres用户)

Installed-Dirs / bin / postmaster -D Installed-Dirs / pgsql / data

举个例子:

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

第二步:从已安装的path运行psql(要检查安装'postgres'将用于找出安装的位置'

 [postgres@localhost bin]$ psql