我尝试将 ruby 连接到 postgres,但无法连接,它显示错误
"uninitialized constant PGconn"
。
require "pg"
conn = PGconn.connect("localhost", 5432, "", "", "test1")
res = conn.exec("select * from a;")
PGConn 错了,postgres gem
pg
现在正在使用 PG
。
示例:
require 'pg'
conn = PG.connect( dbname: 'sales' )
conn.exec( "SELECT * FROM pg_stat_activity" ) do |result|
result.each do |row|
puts row.values_at('procpid', 'usename', 'current_query')
end
end
或者:
require 'pg'
pg_connection = PG::Connection.new(host: 'localhost', user: 'postgres', password: 'postgres')
Connection.new
文档。