1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
9 * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.54 2004/10/16 03:10:15 momjian Exp $
11 *-------------------------------------------------------------------------
14 #include "postgres_fe.h"
26 #include "getopt_long.h"
32 #include "dumputils.h"
34 #include "pg_backup.h"
35 #include "pqexpbuffer.h"
37 #define _(x) gettext((x))
39 /* version string we expect back from postgres */
40 #define PG_VERSIONSTR "pg_dump (PostgreSQL) " PG_VERSION "\n"
43 static const char *progname;
45 static void help(void);
47 static void dumpUsers(PGconn *conn, bool initdbonly);
48 static void dumpGroups(PGconn *conn);
49 static void dumpTablespaces(PGconn *conn);
50 static void dumpCreateDB(PGconn *conn);
51 static void dumpDatabaseConfig(PGconn *conn, const char *dbname);
52 static void dumpUserConfig(PGconn *conn, const char *username);
53 static void makeAlterConfigCommand(const char *arrayitem, const char *type, const char *name);
54 static void dumpDatabases(PGconn *conn);
55 static void dumpTimestamp(char *msg);
57 static int runPgDump(const char *dbname);
58 static PGconn *connectDatabase(const char *dbname, const char *pghost, const char *pgport,
59 const char *pguser, bool require_password);
60 static PGresult *executeQuery(PGconn *conn, const char *query);
62 char pg_dump_bin[MAXPGPATH];
63 PQExpBuffer pgdumpopts;
64 bool output_clean = false;
65 bool skip_acls = false;
69 /* flags for -X long options */
70 int disable_dollar_quoting = 0;
71 int disable_triggers = 0;
72 int use_setsessauth = 0;
75 main(int argc, char *argv[])
80 bool force_password = false;
81 bool data_only = false;
82 bool globals_only = false;
83 bool schema_only = false;
88 static struct option long_options[] = {
89 {"data-only", no_argument, NULL, 'a'},
90 {"clean", no_argument, NULL, 'c'},
91 {"inserts", no_argument, NULL, 'd'},
92 {"attribute-inserts", no_argument, NULL, 'D'},
93 {"column-inserts", no_argument, NULL, 'D'},
94 {"globals-only", no_argument, NULL, 'g'},
95 {"host", required_argument, NULL, 'h'},
96 {"ignore-version", no_argument, NULL, 'i'},
97 {"oids", no_argument, NULL, 'o'},
98 {"no-owner", no_argument, NULL, 'O'},
99 {"port", required_argument, NULL, 'p'},
100 {"password", no_argument, NULL, 'W'},
101 {"schema-only", no_argument, NULL, 's'},
102 {"superuser", required_argument, NULL, 'S'},
103 {"username", required_argument, NULL, 'U'},
104 {"verbose", no_argument, NULL, 'v'},
105 {"no-privileges", no_argument, NULL, 'x'},
106 {"no-acl", no_argument, NULL, 'x'},
109 * the following options don't have an equivalent short option
110 * letter, but are available as '-X long-name'
112 {"disable-dollar-quoting", no_argument, &disable_dollar_quoting, 1},
113 {"disable-triggers", no_argument, &disable_triggers, 1},
114 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
121 set_pglocale_pgservice(argv[0], "pg_dump");
123 progname = get_progname(argv[0]);
127 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
132 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
134 puts("pg_dumpall (PostgreSQL) " PG_VERSION);
139 if ((ret = find_other_exec(argv[0], "pg_dump", PG_VERSIONSTR,
142 char full_path[MAXPGPATH];
144 if (find_my_exec(argv[0], full_path) < 0)
145 StrNCpy(full_path, progname, MAXPGPATH);
149 _("The program \"pg_dump\" is needed by %s "
150 "but was not found in the\n"
151 "same directory as \"%s\".\n"
152 "Check your installation.\n"),
153 progname, full_path);
156 _("The program \"pg_dump\" was found by \"%s\"\n"
157 "but was not the same version as %s.\n"
158 "Check your installation.\n"),
159 full_path, progname);
163 pgdumpopts = createPQExpBuffer();
165 while ((c = getopt_long(argc, argv, "acdDgh:ioOp:sS:U:vWxX:", long_options, &optindex)) != -1)
171 appendPQExpBuffer(pgdumpopts, " -a");
180 appendPQExpBuffer(pgdumpopts, " -%c", c);
190 appendPQExpBuffer(pgdumpopts, " -h '%s'", pghost);
192 appendPQExpBuffer(pgdumpopts, " -h \"%s\"", pghost);
201 appendPQExpBuffer(pgdumpopts, " -%c", c);
205 appendPQExpBuffer(pgdumpopts, " -O");
211 appendPQExpBuffer(pgdumpopts, " -p '%s'", pgport);
213 appendPQExpBuffer(pgdumpopts, " -p \"%s\"", pgport);
219 appendPQExpBuffer(pgdumpopts, " -s");
224 appendPQExpBuffer(pgdumpopts, " -S '%s'", optarg);
226 appendPQExpBuffer(pgdumpopts, " -S \"%s\"", optarg);
233 appendPQExpBuffer(pgdumpopts, " -U '%s'", pguser);
235 appendPQExpBuffer(pgdumpopts, " -U \"%s\"", pguser);
241 appendPQExpBuffer(pgdumpopts, " -v");
245 force_password = true;
246 appendPQExpBuffer(pgdumpopts, " -W");
251 appendPQExpBuffer(pgdumpopts, " -x");
255 if (strcmp(optarg, "disable-dollar-quoting") == 0)
256 appendPQExpBuffer(pgdumpopts, " -X disable-dollar-quoting");
257 else if (strcmp(optarg, "disable-triggers") == 0)
258 appendPQExpBuffer(pgdumpopts, " -X disable-triggers");
259 else if (strcmp(optarg, "use-set-session-authorization") == 0)
260 /* no-op, still allowed for compatibility */ ;
264 _("%s: invalid -X option -- %s\n"),
266 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
275 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
280 /* Add long options to the pg_dump argument list */
281 if (disable_dollar_quoting)
282 appendPQExpBuffer(pgdumpopts, " -X disable-dollar-quoting");
283 if (disable_triggers)
284 appendPQExpBuffer(pgdumpopts, " -X disable-triggers");
286 appendPQExpBuffer(pgdumpopts, " -X use-set-session-authorization");
290 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
291 progname, argv[optind]);
292 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
298 conn = connectDatabase("template1", pghost, pgport, pguser, force_password);
300 printf("--\n-- PostgreSQL database cluster dump\n--\n\n");
302 dumpTimestamp("Started on");
304 printf("\\connect \"template1\"\n\n");
308 /* Dump all users excluding the initdb user */
309 dumpUsers(conn, false);
311 if (server_version >= 80000)
312 dumpTablespaces(conn);
315 /* Dump alter command for initdb user */
316 dumpUsers(conn, true);
325 dumpTimestamp("Completed on");
326 printf("--\n-- PostgreSQL database cluster dump complete\n--\n\n");
336 printf(_("%s extracts a PostgreSQL database cluster into an SQL script file.\n\n"), progname);
337 printf(_("Usage:\n"));
338 printf(_(" %s [OPTION]...\n"), progname);
340 printf(_("\nGeneral options:\n"));
341 printf(_(" -i, --ignore-version proceed even when server version mismatches\n"
342 " pg_dumpall version\n"));
343 printf(_(" --help show this help, then exit\n"));
344 printf(_(" --version output version information, then exit\n"));
345 printf(_("\nOptions controlling the output content:\n"));
346 printf(_(" -a, --data-only dump only the data, not the schema\n"));
347 printf(_(" -c, --clean clean (drop) databases prior to create\n"));
348 printf(_(" -d, --inserts dump data as INSERT, rather than COPY, commands\n"));
349 printf(_(" -D, --column-inserts dump data as INSERT commands with column names\n"));
350 printf(_(" -g, --globals-only dump only global objects, no databases\n"));
351 printf(_(" -o, --oids include OIDs in dump\n"));
352 printf(_(" -O, --no-owner do not output commands to set object ownership\n"));
353 printf(_(" -s, --schema-only dump only the schema, no data\n"));
354 printf(_(" -S, --superuser=NAME specify the superuser user name to use in the dump\n"));
355 printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
356 printf(_(" -X disable-dollar-quoting, --disable-dollar-quoting\n"
357 " disable dollar quoting, use SQL standard quoting\n"));
358 printf(_(" -X disable-triggers, --disable-triggers\n"
359 " disable triggers during data-only restore\n"));
360 printf(_(" -X use-set-session-authorization, --use-set-session-authorization\n"
361 " use SESSION AUTHORIZATION commands instead of\n"
362 " OWNER TO commands\n"));
364 printf(_("\nConnection options:\n"));
365 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
366 printf(_(" -p, --port=PORT database server port number\n"));
367 printf(_(" -U, --username=NAME connect as specified database user\n"));
368 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
370 printf(_("\nThe SQL script will be written to the standard output.\n\n"));
371 printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
378 * Is able to dump all non initdb users or just the initdb user.
381 dumpUsers(PGconn *conn, bool initdbonly)
386 if (server_version >= 70100)
387 res = executeQuery(conn,
388 "SELECT usename, usesysid, passwd, usecreatedb, "
389 "usesuper, valuntil, "
390 "(usesysid = (SELECT datdba FROM pg_database WHERE datname = 'template0')) AS clusterowner "
393 res = executeQuery(conn,
394 "SELECT usename, usesysid, passwd, usecreatedb, "
395 "usesuper, valuntil, "
396 "(usesysid = (SELECT datdba FROM pg_database WHERE datname = 'template1')) AS clusterowner "
399 if (PQntuples(res) > 0 || (!initdbonly && output_clean))
400 printf("--\n-- Users\n--\n\n");
401 if (!initdbonly && output_clean)
402 printf("DELETE FROM pg_shadow WHERE usesysid <> (SELECT datdba FROM pg_database WHERE datname = 'template0');\n\n");
404 for (i = 0; i < PQntuples(res); i++)
406 const char *username;
408 PQExpBuffer buf = createPQExpBuffer();
410 username = PQgetvalue(res, i, 0);
411 clusterowner = (strcmp(PQgetvalue(res, i, 6), "t") == 0);
413 /* Check which pass we're on */
414 if ((initdbonly && !clusterowner) || (!initdbonly && clusterowner))
418 * Dump ALTER USER for the cluster owner and CREATE USER for all
422 appendPQExpBuffer(buf, "CREATE USER %s WITH SYSID %s",
424 PQgetvalue(res, i, 1));
426 appendPQExpBuffer(buf, "ALTER USER %s WITH",
429 if (!PQgetisnull(res, i, 2))
431 appendPQExpBuffer(buf, " PASSWORD ");
432 appendStringLiteral(buf, PQgetvalue(res, i, 2), true);
435 if (strcmp(PQgetvalue(res, i, 3), "t") == 0)
436 appendPQExpBuffer(buf, " CREATEDB");
438 appendPQExpBuffer(buf, " NOCREATEDB");
440 if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
441 appendPQExpBuffer(buf, " CREATEUSER");
443 appendPQExpBuffer(buf, " NOCREATEUSER");
445 if (!PQgetisnull(res, i, 5))
446 appendPQExpBuffer(buf, " VALID UNTIL '%s'",
447 PQgetvalue(res, i, 5));
449 appendPQExpBuffer(buf, ";\n");
451 printf("%s", buf->data);
452 destroyPQExpBuffer(buf);
454 if (server_version >= 70300)
455 dumpUserConfig(conn, username);
468 dumpGroups(PGconn *conn)
473 res = executeQuery(conn, "SELECT groname, grosysid, grolist FROM pg_group");
475 if (PQntuples(res) > 0 || output_clean)
476 printf("--\n-- Groups\n--\n\n");
478 printf("DELETE FROM pg_group;\n\n");
480 for (i = 0; i < PQntuples(res); i++)
482 PQExpBuffer buf = createPQExpBuffer();
486 appendPQExpBuffer(buf, "CREATE GROUP %s WITH SYSID %s;\n",
487 fmtId(PQgetvalue(res, i, 0)),
488 PQgetvalue(res, i, 1));
490 val = strdup(PQgetvalue(res, i, 2));
491 tok = strtok(val, ",{}");
495 PQExpBuffer buf2 = createPQExpBuffer();
498 appendPQExpBuffer(buf2, "SELECT usename FROM pg_shadow WHERE usesysid = %s;", tok);
499 res2 = executeQuery(conn, buf2->data);
500 destroyPQExpBuffer(buf2);
502 for (j = 0; j < PQntuples(res2); j++)
504 appendPQExpBuffer(buf, "ALTER GROUP %s ", fmtId(PQgetvalue(res, i, 0)));
505 appendPQExpBuffer(buf, "ADD USER %s;\n", fmtId(PQgetvalue(res2, j, 0)));
510 tok = strtok(NULL, "{},");
514 printf("%s", buf->data);
515 destroyPQExpBuffer(buf);
526 dumpTablespaces(PGconn *conn)
532 * Get all tablespaces except built-in ones (which we assume are named
535 res = executeQuery(conn, "SELECT spcname, "
536 "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
537 "spclocation, spcacl "
538 "FROM pg_catalog.pg_tablespace "
539 "WHERE spcname NOT LIKE 'pg\\_%'");
541 if (PQntuples(res) > 0)
542 printf("--\n-- Tablespaces\n--\n\n");
544 for (i = 0; i < PQntuples(res); i++)
546 PQExpBuffer buf = createPQExpBuffer();
547 char *spcname = PQgetvalue(res, i, 0);
548 char *spcowner = PQgetvalue(res, i, 1);
549 char *spclocation = PQgetvalue(res, i, 2);
550 char *spcacl = PQgetvalue(res, i, 3);
553 /* needed for buildACLCommands() */
554 fspcname = strdup(fmtId(spcname));
557 appendPQExpBuffer(buf, "DROP TABLESPACE %s;\n", fspcname);
559 appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
560 appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
562 appendPQExpBuffer(buf, " LOCATION ");
563 appendStringLiteral(buf, spclocation, true);
564 appendPQExpBuffer(buf, ";\n");
567 !buildACLCommands(fspcname, "TABLESPACE", spcacl, spcowner,
568 server_version, buf))
570 fprintf(stderr, _("%s: could not parse ACL list (%s) for tablespace \"%s\"\n"),
571 progname, spcacl, fspcname);
576 printf("%s", buf->data);
579 destroyPQExpBuffer(buf);
587 * Dump commands to create each database.
589 * To minimize the number of reconnections (and possibly ensuing
590 * password prompts) required by the output script, we emit all CREATE
591 * DATABASE commands during the initial phase of the script, and then
592 * run pg_dump for each database to dump the contents of that
593 * database. We skip databases marked not datallowconn, since we'd be
594 * unable to connect to them anyway (and besides, we don't want to
598 dumpCreateDB(PGconn *conn)
603 printf("--\n-- Database creation\n--\n\n");
605 if (server_version >= 80000)
606 res = executeQuery(conn,
608 "coalesce(usename, (select usename from pg_shadow where usesysid=(select datdba from pg_database where datname='template0'))), "
609 "pg_encoding_to_char(d.encoding), "
610 "datistemplate, datacl, "
611 "(SELECT spcname FROM pg_tablespace t WHERE t.oid = d.dattablespace) AS dattablespace "
612 "FROM pg_database d LEFT JOIN pg_shadow u ON (datdba = usesysid) "
613 "WHERE datallowconn ORDER BY 1");
614 else if (server_version >= 70300)
615 res = executeQuery(conn,
617 "coalesce(usename, (select usename from pg_shadow where usesysid=(select datdba from pg_database where datname='template0'))), "
618 "pg_encoding_to_char(d.encoding), "
619 "datistemplate, datacl, "
620 "'pg_default' AS dattablespace "
621 "FROM pg_database d LEFT JOIN pg_shadow u ON (datdba = usesysid) "
622 "WHERE datallowconn ORDER BY 1");
623 else if (server_version >= 70100)
624 res = executeQuery(conn,
627 "(select usename from pg_shadow where usesysid=datdba), "
628 "(select usename from pg_shadow where usesysid=(select datdba from pg_database where datname='template0'))), "
629 "pg_encoding_to_char(d.encoding), "
630 "datistemplate, '' as datacl, "
631 "'pg_default' AS dattablespace "
632 "FROM pg_database d "
633 "WHERE datallowconn ORDER BY 1");
637 * Note: 7.0 fails to cope with sub-select in COALESCE, so just
638 * deal with getting a NULL by not printing any OWNER clause.
640 res = executeQuery(conn,
642 "(select usename from pg_shadow where usesysid=datdba), "
643 "pg_encoding_to_char(d.encoding), "
644 "'f' as datistemplate, "
646 "'pg_default' AS dattablespace "
647 "FROM pg_database d "
651 for (i = 0; i < PQntuples(res); i++)
654 char *dbname = PQgetvalue(res, i, 0);
655 char *dbowner = PQgetvalue(res, i, 1);
656 char *dbencoding = PQgetvalue(res, i, 2);
657 char *dbistemplate = PQgetvalue(res, i, 3);
658 char *dbacl = PQgetvalue(res, i, 4);
659 char *dbtablespace = PQgetvalue(res, i, 5);
662 if (strcmp(dbname, "template1") == 0)
665 buf = createPQExpBuffer();
667 /* needed for buildACLCommands() */
668 fdbname = strdup(fmtId(dbname));
671 appendPQExpBuffer(buf, "DROP DATABASE %s;\n", fdbname);
673 appendPQExpBuffer(buf, "CREATE DATABASE %s", fdbname);
675 appendPQExpBuffer(buf, " WITH TEMPLATE = template0");
677 if (strlen(dbowner) != 0)
678 appendPQExpBuffer(buf, " OWNER = %s",
681 appendPQExpBuffer(buf, " ENCODING = ");
682 appendStringLiteral(buf, dbencoding, true);
684 /* Output tablespace if it isn't default */
685 if (strcmp(dbtablespace, "pg_default") != 0)
686 appendPQExpBuffer(buf, " TABLESPACE = %s",
687 fmtId(dbtablespace));
689 appendPQExpBuffer(buf, ";\n");
691 if (strcmp(dbistemplate, "t") == 0)
693 appendPQExpBuffer(buf, "UPDATE pg_database SET datistemplate = 't' WHERE datname = ");
694 appendStringLiteral(buf, dbname, true);
695 appendPQExpBuffer(buf, ";\n");
699 !buildACLCommands(fdbname, "DATABASE", dbacl, dbowner,
700 server_version, buf))
702 fprintf(stderr, _("%s: could not parse ACL list (%s) for database \"%s\"\n"),
703 progname, dbacl, fdbname);
708 printf("%s", buf->data);
709 destroyPQExpBuffer(buf);
712 if (server_version >= 70300)
713 dumpDatabaseConfig(conn, dbname);
723 * Dump database-specific configuration
726 dumpDatabaseConfig(PGconn *conn, const char *dbname)
728 PQExpBuffer buf = createPQExpBuffer();
735 printfPQExpBuffer(buf, "SELECT datconfig[%d] FROM pg_database WHERE datname = ", count);
736 appendStringLiteral(buf, dbname, true);
737 appendPQExpBuffer(buf, ";");
739 res = executeQuery(conn, buf->data);
740 if (!PQgetisnull(res, 0, 0))
742 makeAlterConfigCommand(PQgetvalue(res, 0, 0), "DATABASE", dbname);
753 destroyPQExpBuffer(buf);
759 * Dump user-specific configuration
762 dumpUserConfig(PGconn *conn, const char *username)
764 PQExpBuffer buf = createPQExpBuffer();
771 printfPQExpBuffer(buf, "SELECT useconfig[%d] FROM pg_shadow WHERE usename = ", count);
772 appendStringLiteral(buf, username, true);
773 appendPQExpBuffer(buf, ";");
775 res = executeQuery(conn, buf->data);
776 if (!PQgetisnull(res, 0, 0))
778 makeAlterConfigCommand(PQgetvalue(res, 0, 0), "USER", username);
789 destroyPQExpBuffer(buf);
795 * Helper function for dumpXXXConfig().
798 makeAlterConfigCommand(const char *arrayitem, const char *type, const char *name)
802 PQExpBuffer buf = createPQExpBuffer();
804 mine = strdup(arrayitem);
805 pos = strchr(mine, '=');
810 appendPQExpBuffer(buf, "ALTER %s %s ", type, fmtId(name));
811 appendPQExpBuffer(buf, "SET %s TO ", fmtId(mine));
814 * Some GUC variable names are 'LIST' type and hence must not be
817 if (strcasecmp(mine, "DateStyle") == 0
818 || strcasecmp(mine, "search_path") == 0)
819 appendPQExpBuffer(buf, "%s", pos + 1);
821 appendStringLiteral(buf, pos + 1, false);
822 appendPQExpBuffer(buf, ";\n");
824 printf("%s", buf->data);
825 destroyPQExpBuffer(buf);
832 * Dump contents of databases.
835 dumpDatabases(PGconn *conn)
840 if (server_version >= 70100)
841 res = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1");
843 res = executeQuery(conn, "SELECT datname FROM pg_database ORDER BY 1");
845 for (i = 0; i < PQntuples(res); i++)
849 char *dbname = PQgetvalue(res, i, 0);
852 fprintf(stderr, _("%s: dumping database \"%s\"...\n"), progname, dbname);
854 printf("\\connect %s\n\n", fmtId(dbname));
855 ret = runPgDump(dbname);
858 fprintf(stderr, _("%s: pg_dump failed on database \"%s\", exiting\n"), progname, dbname);
869 * Run pg_dump on dbname.
872 runPgDump(const char *dbname)
874 PQExpBuffer cmd = createPQExpBuffer();
879 * Win32 has to use double-quotes for args, rather than single quotes.
880 * Strangely enough, this is the only place we pass a database name on
881 * the command line, except template1 that doesn't need quoting.
884 appendPQExpBuffer(cmd, "%s\"%s\" %s -Fp '", SYSTEMQUOTE, pg_dump_bin,
886 appendPQExpBuffer(cmd, "%s\"%s\" %s -Fp \"", SYSTEMQUOTE, pg_dump_bin,
890 /* Shell quoting is not quite like SQL quoting, so can't use fmtId */
891 for (p = dbname; *p; p++)
895 appendPQExpBuffer(cmd, "'\"'\"'");
898 appendPQExpBuffer(cmd, "\\\"");
901 appendPQExpBufferChar(cmd, *p);
905 appendPQExpBufferChar(cmd, '\'');
907 appendPQExpBufferChar(cmd, '"');
910 appendPQExpBuffer(cmd, "%s", SYSTEMQUOTE);
913 fprintf(stderr, _("%s: running \"%s\"\n"), progname, cmd->data);
918 ret = system(cmd->data);
920 destroyPQExpBuffer(cmd);
928 * Make a database connection with the given parameters. An
929 * interactive password prompt is automatically issued if required.
932 connectDatabase(const char *dbname, const char *pghost, const char *pgport,
933 const char *pguser, bool require_password)
936 char *password = NULL;
937 bool need_pass = false;
938 const char *remoteversion_str;
940 if (require_password)
941 password = simple_prompt("Password: ", 100, false);
944 * Start the connection. Loop until we have a password if requested
950 conn = PQsetdbLogin(pghost, pgport, NULL, NULL, dbname, pguser, password);
954 fprintf(stderr, _("%s: could not connect to database \"%s\"\n"),
959 if (PQstatus(conn) == CONNECTION_BAD &&
960 strcmp(PQerrorMessage(conn), PQnoPasswordSupplied) == 0 &&
967 password = simple_prompt("Password: ", 100, false);
974 /* check to see that the backend connection was successfully made */
975 if (PQstatus(conn) == CONNECTION_BAD)
977 fprintf(stderr, _("%s: could not connect to database \"%s\": %s\n"),
978 progname, dbname, PQerrorMessage(conn));
982 remoteversion_str = PQparameterStatus(conn, "server_version");
983 if (!remoteversion_str)
985 fprintf(stderr, _("%s: could not get server version\n"), progname);
988 server_version = parse_version(remoteversion_str);
989 if (server_version < 0)
991 fprintf(stderr, _("%s: could not parse server version \"%s\"\n"),
992 progname, remoteversion_str);
1002 * Run a query, return the results, exit program on failure.
1005 executeQuery(PGconn *conn, const char *query)
1010 fprintf(stderr, _("%s: executing %s\n"), progname, query);
1012 res = PQexec(conn, query);
1014 PQresultStatus(res) != PGRES_TUPLES_OK)
1016 fprintf(stderr, _("%s: query failed: %s"), progname, PQerrorMessage(conn));
1017 fprintf(stderr, _("%s: query was: %s\n"), progname, query);
1030 dumpTimestamp(char *msg)
1033 time_t now = time(NULL);
1035 if (strftime(buf, 256, "%Y-%m-%d %H:%M:%S %Z", localtime(&now)) != 0)
1036 printf("-- %s %s\n\n", msg, buf);