简体   繁体   中英

How to Connect the Ruby to the Postgresql?

I try to connect The ruby to the postgres, but it cannot connecting ,it shows an error "uninitialized constant PGconn" .

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

PGConn is wrong, the postrges gem called pg is using PG now.

Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM