本文翻译自:Postgres could not connect to server

After I did brew update and brew upgrade, my postgres got some problem. 在我做了brew更新和brew升级之后,我的postgres遇到了一些问题。 I tried to uninstall postgres and install again, but it didn't work as well. 我试图卸载postgres并重新安装,但它也没有用。

This is the error message.(I also got this error message when I try to do rake db:migrate) 这是错误消息。(当我尝试执行rake db:migrate时,我也收到此错误消息)

$ psql
psql: could not connect to server: No such file or directoryIs the server running locally and acceptingconnections on Unix domain socket "/tmp/.s.PGSQL.5432"?

How can I solve it? 我该如何解决?

Mac version: Mountain lion. Mac版:山狮。

homebrew version: 0.9.3 自制版:0.9.3

postgres version: psql (PostgreSQL) 9.2.1 postgres版本:psql(PostgreSQL)9.2.1

And this is what I did. 这就是我所做的。

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 directoryIs the server running locally and acceptingconnections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Now, after I reinstalled howbrew,when I use $ psql , It doesn't show any error message. 现在,在我重新安装了问题之后,当我使用$ psql ,它没有显示任何错误消息。

But I run rake db:migrate in my rails app, it shows: 但我在我的rails应用程序中运行rake db:migrate ,它显示:

could not connect to server: No such file or directoryIs the server running locally and acceptingconnections 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/

This solution is little tricky, but it works. 这个解决方案有点棘手,但它确实有效。 Hope anyone have a better solution 希望有人有更好的解决方案

Update 更新

This works for me as well. 这对我也有用。

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

#1楼

参考:https://stackoom.com/question/uGjO/Postgres无法连接到服务器


#2楼

Check that the socket file exists. 检查套接字文件是否存在。

$ ls -l /tmp/.s.PGSQL.5432
srwxrwxrwx  1 you  wheel  0 Nov 16 09:22 /tmp/.s.PGSQL.5432

If it doesn't then check your postgresql.conf for unix_socket_directory change. 如果没有,则检查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

#3楼

For anyone reading this and using Postgres.app, you may need host: localhost in your database.yml. 对于阅读此内容并使用Postgres.app的任何人,您可能需要在database.yml中使用host: localhost http://postgresapp.com/documentation#toc_3 http://postgresapp.com/documentation#toc_3


#4楼

Changing postresql or database.yml config settings, changing $PATH, or creating symlinks were all unnecessary for me. 更改postresql或database.yml配置设置,更改$ PATH或创建符号链接对我来说都是不必要的。 All I needed to do was gem uninstall pg and then bundle (or gem install pg ). 我需要做的就是gem uninstall pg然后bundle (或gem install pg )。

The issue was that the pg gem had been installed before homebrew postgres, so was picking up the settings from the version of postgres that comes with MacOS. 问题是pg gem已经在自制的postgres之前安装了,所以从MacOS附带的postgres版本中获取了设置。 Reinstalling it (and thus rebuilding the native extension) fixed the problem. 重新安装它(从而重建原生扩展)修复了问题。


#5楼

Had a similar problem; 有类似的问题; a pid file was blocking postgres from starting up. 一个pid文件阻止postgres启动。 To fix it: 要解决这个问题:

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

and then all is well. 然后一切都很好。


#6楼

I ran into this issue after trying to restore/drop/create a db while other processes were accessing them. 在尝试恢复/删除/创建数据库时,我遇到了这个问题,而其他进程正在访问它们。 MacOSX/Homebrew fix was: MacOSX / Homebrew修复是:

  1. close all other accessing processes rails server , rails console , guard , etc... 关闭所有其他访问进程rails serverrails consoleguard等...
  2. load/unload using the commands found in brew info postgres 使用brew info postgres的命令加载/卸载
  3. run restore/drop/create from before 从之前运行restore / drop / create

Postgres无法连接到服务器相关推荐

  1. Navicat是如何连接到服务器的呢

    2019独角兽企业重金招聘Python工程师标准>>> Navicat的功能足以符合专业开发人员的所有需求,而且对数据库服务器的新手来说又相当容易学习.有了极完备的图形用户界面 (G ...

  2. 修改了sql默认路径无法登录服务器,PostgreSQL错误'无法连接到服务器:没有这样的文件或目录'...

    像其他一些人一样,当我在我的项目中运行rake db:migrate或者甚至为我的Ruby on Rails 3.2应用程序尝试大多数数据库任务时,我收到此错误. PGError(无法连接到服务器:没 ...

  3. 连接服务器显示凭据,连接云服务器时的凭据是什么意思

    连接云服务器时的凭据是什么意思 内容精选 换一换 网站的访问与域名的状态.域名实名认证状态.网站备案状态.解析是否生效.网站网络环境等多个环节有关系.在这些环节中,任意一个环节出现问题,都会导致网站无 ...

  4. navicat怎么连接远程oracle,Navicat如何连接远程服务器?

    不管通过SSL.SSH或HTTP,要成功地创建一个新的连接到本机或远程服务器,都要在Navicat常规选项卡中设置连接属性.如果你的互联网服务供应商(ISP)不提供直接访问其服务器,安全通道协定(SS ...

  5. 用友链接服务器文件,用友t3的数据库怎么连接到服务器

    用友t3的数据库怎么连接到服务器 内容精选 换一换 华为云帮助中心,为用户提供产品简介.价格说明.购买指南.用户指南.API参考.最佳实践.常见问题.视频帮助等技术文档,帮助您快速上手使用华为云服务. ...

  6. windows上使用DataStudio远程连接linux服务器的PosqtgreSQL

    windows上使用DataStudio远程连接linux服务器的PosqtgreSQL 1.环境 2.linux上安装PosqtgreSQL步骤 2.1 使用yum安装 2.2 初始化数据库 2.3 ...

  7. esp8266监听服务器端口信息,esp8266连接远程服务器收信息

    esp8266连接远程服务器收信息 内容精选 换一换 华为云帮助中心,为用户提供产品简介.价格说明.购买指南.用户指南.API参考.最佳实践.常见问题.视频帮助等技术文档,帮助您快速上手使用华为云服务 ...

  8. pycharm连接远程服务器并进行代码上传+远程调试

    Pycharm连接远程服务器并进行代码上传+远程调试 </h1><div class="clear"></div><div class=& ...

  9. 连接远程服务器CredSSP加密Oracle修正报错解决办法

    连接远程服务器CredSSP加密Oracle修正报错解决办法: 打开注册表,快捷输入 "regedit"(类似找命令提示符 输入 cmd 一样)找文件夹 路径:HKLM(缩写)\S ...

最新文章

  1. Linux TCPIP内核参数优化
  2. 【机器学习】集成学习ensemble之随机森林
  3. 【离散数学】两类有限状态自动机
  4. PyTorch学习—14.PyTorch中的学习率调整策略
  5. AUTOCAD参数约束功能
  6. 水下无线光通信关键技术与未来展望
  7. Mbed TLS 编码规范
  8. 设计一个密码登录程序。要求: 设定用户名为lili,密码是123321。若用户名正确,密码也正确,则显示:“lili,欢迎您”
  9. pandas警告SettingWithCopyWarning: A value is trying to ...原理和解决方案
  10. 刨根问底!!到底什么是文件?什么是流
  11. 中文转拼音 中文转罗马声调
  12. AMR-WB 比特流---单通道多帧,120ms RTP打包
  13. 【老九学堂】【C语言进阶】递归调用
  14. 解决 Invalid component name: “404“. Component names should conform to valid custom element name ...
  15. 消息队列技术的介绍和原理(MQ)
  16. 团体程序设计天梯赛 L1-题目
  17. java中的集合详解
  18. 怎样解决张力控制的难点?
  19. html中字体纹理效果,CSS3实现各种纹理背景效果
  20. WinForm删除边框、圆角边框

热门文章

  1. 算法模板——线段树6(二维线段树:区域加法+区域求和)(求助phile)
  2. 网络学习(一)网络版块主题介绍
  3. GdiPlus[16]: IGPLinearGradientBrush 之 SetBlendBellShape、SetBlendTriangularShape
  4. Centos7搭建LAMP+Typecho博客
  5. 5.Django|模型层--多表关系
  6. 【搜索】【广搜模板】
  7. 一、什么是类,一些关键字
  8. 运行eclipse java virtual machine launcher 什么错误
  9. Eclipse用法与技巧——导入工程时报错(already exist in the workspace)
  10. MySQL 5.6 主从报错一例