Rempargo Rempargo - 4 months ago 14
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 192.168.0.100 --port 5432 --username postgres --list

Answer

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;