使用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
相关产品推荐
相关产品推荐

