You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PG gem连接Rails应用与远程PostgreSQL数据库失败问题

Rails应用通过PG gem连接远程PostgreSQL数据库问题

已配置的database.yaml文件

development:
  adapter: postgresql
  encoding: unicode
  database: testdb
  username: testuser
  password: "*******"
  host: **.***.***.***
  port: 5432

控制器中使用的错误连接代码

conn = PG.connect( :dbname => 'testdb' )

收到的错误响应

"status": "error",
"message": "could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket \"/var/run/postgresql/.s.PGSQL.5432\"?\n"

确认Rails已加载数据库配置

检查后确认database.yaml的参数已被Rails加载:

#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd5ae0562d0 @env_name="development", @name="primary", @configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :database=>"testdb", :username=>"testuser", :password=>"*****", :host=>"**.***.***.***", :port=>5432}>

可成功连接的代码写法

只有传入完整的连接参数或连接字符串时才能成功连接:

conn = PG::Connection.new( "postgresql://testuser:*******@**.***.***.***:5432/testdb" )

内容的提问来源于stack exchange,提问作者Ted

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 17:50:29