0

I am using Rails 4. I am trying to list all PostgreSQL databases with '\l' using 'execute' method from ActiveRecord. Connection is correctly established.

p = ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(configuration)
p.send 'establish_master_connection'
p.connection.execute('\l')

Here is the error:

ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR: syntax error at or near "l"

When I use 'no slash' method it works fine

connection.execute("DROP DATABASE IF EXISTS NOTHING")
=> #<PG::Result:0xc5e6094 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=0>

Any idea?

1
  • \l is not a SQL statement, it's a command specific to psql. The only "interface" through which you can run that is psql Commented Mar 31, 2015 at 15:01

1 Answer 1

1

You can't. Commands beginning with \ are meta commands implemented by the psql shell itself - the database server itself doesn't know what they mean.

In the particular case of the commands listing various things, this normally boils down to querying tables in pg_catalog e.g.

SELECT * FROM pg_catalog.pg_database 

for databases

SELECT * FROM pg_catalog.pg_table

for tables. These are documented here

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to list databases then?
@PiotrBrudny You need to do the query that psql was doing for you - i've given some examples

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.