在rails控制台上,当我尝试连接到postgres数据库时,在具有ip地址的机器上假设xxx.xxx.xxx.xxx。
安装宝石
gem 'dbi'
sudo apt-get install libpq-dev
gem 'dbd-pg'
我还授予了'postgres'用户的所有权限,并在此xxx.xxx.xxx.xxx计算机上重新启动了postgres服务器
GRANT ALL PRIVILEGES ON DATABASE test TO postgres WITH GRANT OPTION
sudo /etc/init.d/postgresql restart
我收到了以下错误。
1.9.3p448:001> dbh = DBI.connect(“DBI:Pg:test:xxx.xxx.xxx.xxx”,“postgres”,“”)
DBI :: OperationalError:无法连接到服务器:连接被拒绝 服务器是否在主机“xxx.xxx.xxx.xxx”上运行并接受TCP / IP 端口5432上的连接?
from /home/ashwini/.rvm/gems/ruby-1.9.3-p448/gems/dbd-pg-0.3.9/lib/dbd/pg/database.rb:82:in `rescue in initialize'
from /home/ashwini/.rvm/gems/ruby-1.9.3-p448/gems/dbd-pg-0.3.9/lib/dbd/pg/database.rb:41:in `initialize'
from /home/ashwini/.rvm/gems/ruby-1.9.3-p448/gems/dbd-pg-0.3.9/lib/dbd/Pg.rb:157:in `new'
from /home/ashwini/.rvm/gems/ruby-1.9.3-p448/gems/dbd-pg-0.3.9/lib/dbd/Pg.rb:157:in `connect'
from /home/ashwini/.rvm/gems/ruby-1.9.3-p448/gems/dbi-0.4.5/lib/dbi/handles/driver.rb:33:in `connect'
from /home/ashwini/.rvm/gems/ruby-1.9.3-p448/gems/dbi-0.4.5/lib/dbi.rb:148:in `connect'
from (irb):1
from /home/ashwini/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
from /home/ashwini/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
from /home/ashwini/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
请告诉我哪里出错了。
谢谢...
答案 0 :(得分:2)
与psql
联系并运行:
SHOW port;
给出的端口不是5432
。你可能也在Mac上;对于mac用户来说,有一些二进制文件链接到操作系统提供的libpq
(旧的PostgreSQL),有些二进制文件链接到他们自己安装的PostgreSQL的libpq
。它们可以具有不同的默认套接字目录和不同的TCP端口默认值。
您还应该:
SHOW listen_addresses ;
并确保它是*
。如果它改为localhost
,那么你的PostgreSQL不会监听外部IP,只会监听本地环回。在这种情况下,您应该在配置文件中更改它。请参阅PostgreSQL介绍文档。
如果您已确认PostgreSQL正在侦听感兴趣的IP和端口,则应确保没有防火墙阻止来自外部计算机的连接。但这通常会导致超时,而不是连接拒绝错误。