Rempargo Rempargo - 3 months ago 6x
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


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;