如何将Ruby连接到Postgresql?

问题描述 投票:0回答:2

我尝试将 ruby 连接到 postgres,但无法连接,它显示错误

"uninitialized constant PGconn"

require "pg"
conn = PGconn.connect("localhost", 5432, "", "", "test1")
res = conn.exec("select * from a;")
ruby
2个回答
5
投票

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

0
投票

或者:

require 'pg'

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

Connection.new
文档

© www.soinside.com 2019 - 2024. All rights reserved.