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.