如何将Ruby连接到PostgreSQL?

oknwwptz  于 12个月前  发布在  Ruby
关注(0)|答案(2)|浏览(100)

我尝试将ruby连接到postgres,但无法连接,显示错误"uninitialized constant PGconn"

require "pg"
conn = PGconn.connect("localhost", 5432, "", "", "test1")
res = conn.exec("select * from a;")

字符串

jc3wubiy

jc3wubiy1#

PGConn是错误的,称为pg的postrges gem现在正在使用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

字符串

5rgfhyps

5rgfhyps2#

或者:

require 'pg'

pg_connection = PG::Connection.new(host: 'localhost', user: 'postgres', password: 'postgres')

字符串
Connection.new文档。

相关问题