Rempargo Rempargo - 1 year ago 84
Ruby Question

How to list all databases with ActiveRecord

I want to be able to list all databases with ActiveRecord.

So I need an ActiveRecord equivalent of the following command in the terminal:

psql --host --port 5432 --username postgres --list

Answer Source

I don't know PostgreSQL, but if there is a SQL query which return this information you can do this:

sql = "select * from ... your sql query here"
records_array = ActiveRecord::Base.connection.execute(sql)

Looks like this is the SQL you need:

SELECT datname FROM pg_database
WHERE datistemplate = false;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download