Paul Verschoor
Paul Verschoor

Reputation: 1559

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

Upvotes: 0

Views: 383

Answers (1)

ceth
ceth

Reputation: 45295

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;

Upvotes: 5

Related Questions