3

Could someone please explain how to obtain a list of all existing databases on a PostgreSQL server, to which the user already has access, using Qt? PostgreSQL documentation suggests the following query:

SELECT datname FROM pg_database WHERE datistemplate = false;

What are the correct parameters to the following functions:

QSqlDatabase::setDatabaseName(const QString & name) //"postgres" or "pg_database"?
QSqlDatabase::setUserName(const QString & name) //actual user name?
QSqlDatabase::setPassword(const QString & password) //no password? or user password?

Much appreciated. Thank you in advance.

1
  • You appear to have already answered the first part of your question. Connect to the postgres or template1 database and issue the query you've quoted above to get a list of databases. Commented Sep 5, 2012 at 0:06

1 Answer 1

2

You appear to have already answered the first part of your question. Connect to the postgres or template1 database and issue the query you've quoted above to get a list of databases. I'm guessing - reading between the lines - that you don't know how to connect to PostgreSQL to send that query, and that's what the second part of your question is about. Right?

If so, the QSqlDatabase accessor functions you've mentioned are used to set connection parameters, so the "correct" values depend on your environment.

If you want to issue the query above - to list databases - then you would probably want to connect to the postgres database as it always exists and isn't generally used for anything specific, it's there just to be connected to. That means you'd call setDatabaseName("postgres");. Passing pg_database to setDatabaseName would be nonsensical, since pg_database is the pg_catalog.pg_database table, it isn't a database you can connect to. pg_database is one of those odd tables that exists in every database, which might be what confused you.

With the other two accessors specify the appropriate username and password for your environment, same as you'd use for psql; there's no possible way I could tell you which ones to use.

Note that if you set a password but one isn't required because authentication is done over unix socket ident, trust, or other non-password scheme the password will be ignored.

If this doesn't cover your question, consider editing it and explaining your problem in more detail. What've you tried? What didn't work how you expected? Error messages? Qt version?

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

4 Comments

I am not familiar with PostgreSQL, but I was able to connect. I was just getting an empty resultset. I am not sure what the problem is, but I will continue investigating. At least I know I am on the right track. Thank you for the thorough response.
@user1157168 First thought: there could be a user-defined table, also named pg_database, that's on the search_path before pg_catalog.pg_database. Unlikely, but who knows. You shouldn't ever get an empty result set from running that query on a normal setup because the postgres database isn't listed as a template database.
It works now as expected. To clarify, there was no need to connect to a specific database at all, so calling QSqlDatabase::setDatabaseName() wasn't necessary at all. Thanks for your help agains.
@user1157168 You might want to mention that to the Qt folks to document, then, because that's counter to how PostgreSQL normally works - you can't do anything with PostgreSQL without connecting to a database, even list databases. They must be assuming postgres as the database if none is specified or something.

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.