Search code examples
c++cpostgresqlc++11libpq

organize the PostgreSQL database connection parameters using libpq?


I'm trying to figure out the correct way to pass database connection parameters to this libpq connection control function.

PQconnectdbParams

PGconn *PQconnectdbParams(const char * const *keywords,
    const char * const *values, int expand_dbname);  

Solution

  • From the documentation:

    This function opens a new database connection using the parameters taken from two NULL-terminated arrays. The first, keywords, is defined as an array of strings, each one being a key word. The second, values, gives the value for each key word.

    I have never used this function in practice (as PQconnectdb() seems simpler), but this example should work I think:

    char *keywords[] = {"hostaddr", "port", "dbname", 0};
    char *values[] = {"127.0.0.1", "5432", "testdb", 0};
    
    conn = PQconnectdbParams((const char **)keywords, (const char **)values, 0);