Question

Just another problem with Soci... I want to connect with my testDB which I've just created but the code below shows fatal error.

I did this:

On PostgreSQL:

1) CREATE USER robert WITH ENCRYPTED PASSWORD 'pass'; 
2) CREATE DATABASE testDB; 
3) GRANT ALL PRIVILEGES ON DATABASE testDB TO robert;

My C++ code:

#include <iostream>
#include <soci.h>
#include <string>
using std::string;
using std::cout;
#include <postgresql/soci-postgresql.h>
 
bool connectToDatabase(string databaseName, string user, string password)
{
        try
   { 
      soci::session sql(soci::postgresql, "dbname=" +databaseName + " user="+user + " password="+password);
   }
   catch (soci::postgresql_soci_error const & e)
   {
      std::cerr << "PostgreSQL error: " << e.sqlstate() << " " << e.what() << std::endl;
        return false;        
   }
   catch (std::exception const & e)
   {
      std::cerr << "Some other error: " << e.what() << std::endl;
        return false;   
        }
        return true;
}
 
int main(int argc, char **argv)
{
   cout << connectToDatabase("testDB", "robert", "pass"); 
 
   return 0;
 
}

after compilation I got this:

Some other error: Cannot establish connection to the database.
FATAL:  database "testDB" does not exist

What's wrong?

Was it helpful?

Solution

Connect to database "testdb" instead.

Case folding in PostgreSQL works by converting unquoted identifiers to lower case. So CREATE DATABASE testDB creates a database called "testdb", in contrast to CREATE DATABASE "testDB".

(As a general piece of advice: either get used to using all-lowercase identifiers, maybe with underscores between words, or get used to quoting identifiers all the time. The former is more natural for PostgreSQL, the latter allows you to stick with your convention despite it).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top