如何使用ActiveRecord列出所有数据库 [英] How to list all databases with ActiveRecord

查看:78
本文介绍了如何使用ActiveRecord列出所有数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够列出所有带有ActiveRecord的数据库。

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

因此,我需要在终端中使用等效于以下命令的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


推荐答案

我不知道PostgreSQL,但是如果有SQL查询返回此信息,则可以执行以下操作:

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)

看起来这是您需要的SQL:

Looks like this is the SQL you need:

SELECT datname FROM pg_database
WHERE datistemplate = false;

这篇关于如何使用ActiveRecord列出所有数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆