1

Currently I am working on a learning by doing project that should use a database to save the data. I am using Qt C++ and I have installed postgres.app.

My first attempt to write a connection method is mainly taken from this source (http://www.youtube.com/watch?v=3XE2bKUAxfw) and looks like this after some modifications:

int database::connect(QString servername, QString dbname){

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");

db.setConnectOptions();

QString dsn = QString("Driver={PostgreSQL};Server=%1;Port=5432;Database=%2;").arg(servername).arg(dbname);

db.setDatabaseName(dsn);

if (db.open()){
    qDebug() << "Opened";

    return 1;

    db.close();
} else {
    qDebug() << "Error = " << db.lastError().text();
    return -1;
}

}

This first method produces the Error: "could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"? QPSQL: Unable to connect"

After I was unable to fix this problem, I decided to start the method from scratch. This is what I came up with after some internet research and reading the Qt Documentation:

int database::nconnect(QString servername, QString dbname){

QSqlDatabase db;
db = (QSqlDatabase::addDatabase("QPSQL"));
db.setHostName(servername);
db.setDatabaseName(dbname);
db.setUserName("janhettenkofer");
db.setPassword("testpw");
bool connectioncheck = db.open("janhettenkofer","testpw");

if (connectioncheck == true){
    qDebug() << "Connection to database established." << endl;
} else {
    qDebug() << "Error for database " << db.databaseName() << " :" << db.lastError().text() << endl;
}
return connectioncheck;

}

This method caused the error "FATAL: password authentication failed for user "janhettenkofer""

I got rid of another installation of PostgreSQL with the method explained here: https://stackoverflow.com/a/20010057. Now I get this error with the second function (it did not change the first error message): "FATAL: database "mydb" does not exist QPSQL: Unable to connect"

In the thread mentioned above another method of fixing the first error was proposed, but when I open the terminal and type "which psql" nothing happens, not even a "command not found" message is shown. And I do not have any idea how to apply the advice concerning the export PATH. Google only tells me that I should do it, but not how. Terminal is another thing I am only learning to use.

Thank you for any ideas or advice on how to use the terminal in advance.

EDIT:

Ok, I found out how to add psql to the PATH. Now I can run which psql. This points me to the Postgres.app installation I am using. After that I created the user database database. I can connect to this database using either Qt or the terminal. However I cannot create any other database. As I understand it you do createdb mydb and then CREATE DATABASE. After I do this \list still only shows the user database, postgres database and template0 and template1. There is no mydb.

Community
  • 1
  • 1
Jan Hettenkofer
  • 139
  • 4
  • 13
  • If "which psql" returns nothing, psql is not in your PATH. which doesn't return an error message in case nothing was found. But yes, first make sure you can connect to your DB with the right credentials, from command line or another UI before continuing with Qt. – Frank Osterfeld Feb 03 '14 at 19:14
  • How'd you install PostgreSQL on your OS X system? – Craig Ringer Feb 04 '14 at 00:46
  • Thanks @FrankOsterfeld, adding psql to the PATH solved some problems (see edit). I have installed Postgres93.app. – Jan Hettenkofer Feb 04 '14 at 13:38

1 Answers1

1

No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"? QPSQL: Unable to connect"

Either PostreSQL isn't running, or (most likely on Mac OS X) your Qt linked to the libpq of the old PostgreSQL supplied as part of Mac OS X, and you're running a newer version you installed yourself. You should specify host as /tmp to force the unix_socket_directory to where your Pg is running. If that's not it, check the correct location by connecting with psql and using SHOW unix_socket_directory or (newer versions) SHOW unix_socket_directories. You should also SHOW port; to see what port PostgreSQL is listening on; this applies for unix socket directories as well as TCP/IP.

The only difference with the second snippet is that you're specifying a hostname, so you're connecting to localhost over TCP/IP, not unix sockets.


The rest of your problems are probably being caused by not understanding the effect that Apple's pre-installed version of PostgreSQL has on your system. I strongly suspect you're running CREATE USER,ALTER USER, CREATE DATABASE etc commands on one database, while your Qt program is connecting to a different PostgreSQL instance.

(My feelings toward Apple for creating this mess are not printable here).

What you need to do is:

  • Confirm the unix_socket_directory and port number the PostgreSQL instance you installed is running on; and then

  • Use those details in your Qt program.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • Thanks @CraigRinger, I changed the host from `localhost` to `tmp`. The second function works now, but I still get the old Error for the first. – Jan Hettenkofer Feb 04 '14 at 13:43
  • @JanHettdnkofer Yes because the first fails to set a host at all and your Qt is linked to a different libpq than thr running PostgreSQL so the default Unix socket directory used if no host is specified is different. Its a Mac OS X thing caused by Apple mis-packaging PostgreSQL in the OS bundle. – Craig Ringer Feb 04 '14 at 14:34