DevHeads.net

Get PG Database Names in IRB using Sequel

Hi folks.

I need to get a list of schemas (databases) in Postgresql using Sequel. My client can perform the SQL, but for some reason, my connection in IRB doesn’t want see anything.

require ‘sequel’
require ‘pg’
d = Sequel.connect('postgres://dingbat: ... at 0 dot 0.0.0/postgres')
r = d.run('select datname, datistemplate from pg_database where datistemplate = false’)
puts r.inspect # => nil

Any workarounds that anybody knows about? I appreciate any input.

Cheers, Bee

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Comments

Re: Get PG Database Names in IRB using Sequel

By Aleksey Ivanov at 09/30/2016 - 01:28

Hi.
It seems like run method is used just to execute SQL. It doesn't provide
you with a result since the method always explicit returns nil (see here
<https://github.com/jeremyevans/sequel/blob/master/lib/sequel/database/query.rb#L76-L80>
).
You can use a dataset like

DB = Sequel.connect('postgres://dingbat: ... at 0 dot 0.0.0/postgres')
databases = DB['select datname, datistemplate from pg_database where
datistemplate = false']
databases.each { |db| p db }

2016-09-30 2:32 GMT+03:00 Bee.Lists <bee. ... at gmail dot com>:

Re: Get PG Database Names in IRB using Sequel

By Bee.Lists at 09/30/2016 - 03:17

Exactly along the lines of which I was ‘using’, but IRB is my first go-to and it was closed. My postgresql client showed otherwise. So that’s the confusion.

databases.size # => nil

Confusing.

Anyway, I hard coded the database (schema) names, and all is good.

Thanks

Cheers, Bee

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>