1/*-------------------------------------------------------------------------
4 * This is a common file connection to the database.
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/bin/pg_dump/connectdb.c
12 *-------------------------------------------------------------------------
29 * Make a database connection with the given parameters. An
30 * interactive password prompt is automatically issued if required.
32 * If fail_on_error is false, we return NULL without printing any message
33 * on failure, but preserve any prompted password for the next try.
35 * On success, the 'connstr' is set to a connection string containing
36 * the options used and 'server_version' is set to version so that caller
41 const char *
pghost,
const char *
pgport,
const char *pguser,
44 char *override_dbname)
48 const char *remoteversion_str;
51 const char **
values = NULL;
53 int server_version_temp;
59 * Start the connection. Loop until we have a password if requested by
74 * Merge the connection info inputs given in form of connection string
75 * and other options. Explicitly discard any dbname value in the
76 * connection string; otherwise, PQconnectdbParams() would interpret
77 * that value as being itself a connection string.
82 if (conn_opts == NULL)
85 for (conn_opt = conn_opts; conn_opt->
keyword != NULL; conn_opt++)
87 if (conn_opt->
val != NULL && conn_opt->
val[0] !=
'0円' &&
88 strcmp(conn_opt->
keyword,
"dbname") != 0)
95 for (conn_opt = conn_opts; conn_opt->
keyword != NULL; conn_opt++)
97 if (conn_opt->
val != NULL && conn_opt->
val[0] !=
'0円' &&
98 strcmp(conn_opt->
keyword,
"dbname") != 0)
149 keywords[
i] =
"fallback_application_name";
162 prompt_password !=
TRI_NO)
170 /* check to see that the backend connection was successfully made */
188 * Ok, connected successfully. If requested, remember the options used, in
189 * the form of a connection string.
200 if (!remoteversion_str)
201 pg_fatal(
"could not get server version");
204 if (server_version_temp == 0)
205 pg_fatal(
"could not parse server version \"%s\"",
208 /* If requested, then copy server version to out variable. */
212 my_version = PG_VERSION_NUM;
215 * We allow the server to be back to 9.2, and up to any minor release of
216 * our own major version. (See also version check in pg_dump.c.)
218 if (my_version != server_version_temp
219 && (server_version_temp < 90200 ||
220 (server_version_temp / 100) > (my_version / 100)))
222 pg_log_error(
"aborting because of server version mismatch");
224 remoteversion_str,
progname, PG_VERSION);
236 * Construct a connection string from the given keyword/value pairs. It is
237 * used to pass the connection options to the pg_dump subprocess.
239 * The following parameters are excluded:
240 * dbname - varies in each pg_dump invocation
241 * password - it's not secure to pass a password on the command line
242 * fallback_application_name - we'll let pg_dump set it
250 bool firstkeyword =
true;
252 /* Construct a new connection string in key='value' format. */
255 if (strcmp(
keywords[
i],
"dbname") == 0 ||
257 strcmp(
keywords[
i],
"fallback_application_name") == 0)
262 firstkeyword =
false;
275 * Run a query, return the results, exit program on failure.
static Datum values[MAXATTR]
#define ALWAYS_SECURE_SEARCH_PATH_SQL
static char * constructConnStr(const char **keywords, const char **values)
PGresult * executeQuery(PGconn *conn, const char *query)
PGconn * ConnectDatabase(const char *dbname, const char *connection_string, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error, const char *progname, const char **connstr, int *server_version, char *password, char *override_dbname)
int PQserverVersion(const PGconn *conn)
void PQconninfoFree(PQconninfoOption *connOptions)
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
int PQconnectionNeedsPassword(const PGconn *conn)
ConnStatusType PQstatus(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
PGresult * PQexec(PGconn *conn, const char *query)
char * pg_strdup(const char *in)
void * pg_malloc0(size_t size)
static const JsonPathKeyword keywords[]
#define pg_log_error(...)
#define pg_log_error_detail(...)
void exit_nicely(int code)
static const char * connstr
static int server_version
static const char * pghost
static const char * pgport
PQExpBuffer createPQExpBuffer(void)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void destroyPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
char * simple_prompt(const char *prompt, bool echo)
void appendConnStrVal(PQExpBuffer buf, const char *str)