使用 libpq 组织 PostgreSQL 数据库连接参数?

organize the PostgreSQL database connection parameters using libpq?

我正在尝试找出将数据库连接参数传递给此 libpq 连接控制函数的正确方法。

PQconnectdbParams

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

来自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.

我从来没有在实践中使用过这个函数(因为 PQconnectdb() 看起来更简单),但我认为这个例子应该有效:

char *keywords[] = {"hostaddr", "port", "dbname", 0};
char *values[] = {"127.0.0.1", "5432", "testdb", 0};

conn = PQconnectdbParams((const char **)keywords, (const char **)values, 0);