1/*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
8 * pg_dumpall forces all pg_dump output to be text, since it also outputs
9 * text into the same output stream.
11 * src/bin/pg_dump/pg_dumpall.c
13 *-------------------------------------------------------------------------
21#include "catalog/pg_authid_d.h"
34/* version string we expect back from pg_dump */
35 #define PGDUMP_VERSIONSTR "pg_dump (PostgreSQL) " PG_VERSION "\n"
44 #define SH_PREFIX rolename
45 #define SH_ELEMENT_TYPE RoleNameEntry
46 #define SH_KEY_TYPE char *
47 #define SH_KEY rolename
48 #define SH_HASH_KEY(tb, key) hash_string(key)
49 #define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0)
51 #define SH_GET_HASH(tb, a) (a)->hashval
52 #define SH_SCOPE static inline
53 #define SH_RAW_ALLOCATOR pg_malloc0
58static void help(
void);
72 const char *catalog_name,
Oid objectId,
73 const char *objtype,
const char *objname,
116 #define PG_AUTHID "pg_authid"
117 #define PG_ROLES "pg_roles "
130 static struct option long_options[] = {
153 * the following options don't have an equivalent short option letter
198 char *use_role = NULL;
199 const char *dumpencoding = NULL;
201 bool data_only =
false;
202 bool globals_only =
false;
203 bool roles_only =
false;
204 bool tablespaces_only =
false;
207 const char *std_strings;
219 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
224 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
226 puts(
"pg_dumpall (PostgreSQL) " PG_VERSION);
240 pg_fatal(
"program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
243 pg_fatal(
"program \"%s\" was found by \"%s\" but was not the same version as %s",
249 while ((
c =
getopt_long(argc, argv,
"acd:E:f:gh:l:Op:rsS:tU:vwWx", long_options, &optindex)) != -1)
312 tablespaces_only =
true;
384 /* getopt_long already emitted a complaint */
390 /* Complain if any arguments remain */
393 pg_log_error(
"too many command-line arguments (first is \"%s\")",
400 (globals_only || roles_only || tablespaces_only))
402 pg_log_error(
"option --exclude-database cannot be used together with -g/--globals-only, -r/--roles-only, or -t/--tablespaces-only");
407 /* Make sure the user hasn't specified a mix of globals-only options */
408 if (globals_only && roles_only)
410 pg_log_error(
"options -g/--globals-only and -r/--roles-only cannot be used together");
415 if (globals_only && tablespaces_only)
417 pg_log_error(
"options -g/--globals-only and -t/--tablespaces-only cannot be used together");
423 pg_fatal(
"option --if-exists requires option -c/--clean");
425 if (roles_only && tablespaces_only)
427 pg_log_error(
"options -r/--roles-only and -t/--tablespaces-only cannot be used together");
433 * If password values are not required in the dump, switch to using
434 * pg_roles which is equally useful, just more likely to have unrestricted
435 * access than pg_authid.
442 /* Add long options to the pg_dump argument list */
493 * If you don't provide a restrict key, one will be appointed for you.
498 pg_fatal(
"could not generate restrict key");
503 * If there was a database specified on the command line, use that,
504 * otherwise try to connect to database "postgres", and failing that
510 prompt_password,
false,
514 pg_fatal(
"could not connect to database \"%s\"", pgdb);
519 prompt_password,
false,
523 prompt_password,
true,
528 pg_log_error(
"could not connect to databases \"postgres\" or \"template1\"\n"
529 "Please specify an alternative database.");
536 * Get a list of database names that match the exclude patterns
542 * Open the output file if required, otherwise use stdout
548 pg_fatal(
"could not open output file \"%s\": %m",
555 * Set the client encoding if requested.
560 pg_fatal(
"invalid client encoding \"%s\" specified",
565 * Get the active encoding and the standard_conforming_strings setting, so
566 * we know how to escape strings.
574 /* Set the role if requested */
584 /* Force quoting of all identifiers if requested. */
588 fprintf(
OPF,
"--\n-- PostgreSQL database cluster dump\n--\n\n");
593 * Enter restricted mode to block any unexpected psql meta-commands. A
594 * malicious source might try to inject a variety of things via bogus
595 * responses to queries. While we cannot prevent such sources from
596 * affecting the destination at restore time, we can block psql
597 * meta-commands so that the client machine that runs psql with the dump
598 * output remains unaffected.
603 * We used to emit \connect postgres here, but that served no purpose
604 * other than to break things for installations without a postgres
605 * database. Everything we're restoring here is a global, so whichever
606 * database we're connected to at the moment is fine.
609 /* Restore will need to write to the target cluster */
610 fprintf(
OPF,
"SET default_transaction_read_only = off;\n\n");
612 /* Replicate encoding and std_strings in output */
615 fprintf(
OPF,
"SET standard_conforming_strings = %s;\n", std_strings);
616 if (strcmp(std_strings,
"off") == 0)
617 fprintf(
OPF,
"SET escape_string_warning = off;\n");
623 * If asked to --clean, do that first. We can avoid detailed
624 * dependency analysis because databases never depend on each other,
625 * and tablespaces never depend on each other. Roles could have
626 * grants to each other, but DROP ROLE will clean those up silently.
630 if (!globals_only && !roles_only && !tablespaces_only)
636 if (!tablespaces_only)
641 * Now create objects as requested. Be careful that option logic here
642 * is the same as for drops above.
644 if (!tablespaces_only)
646 /* Dump roles (users) */
649 /* Dump role memberships */
652 /* Dump role GUC privileges */
657 /* Dump tablespaces */
663 * Exit restricted mode just before dumping the databases. pg_dump will
664 * handle entering restricted mode again as appropriate.
668 if (!globals_only && !roles_only && !tablespaces_only)
675 fprintf(
OPF,
"--\n-- PostgreSQL database cluster dump complete\n--\n\n");
681 /* sync the resulting file, errors are not fatal */
693 printf(
_(
"%s exports a PostgreSQL database cluster as an SQL script.\n\n"),
progname);
697 printf(
_(
"\nGeneral options:\n"));
698 printf(
_(
" -f, --file=FILENAME output file name\n"));
699 printf(
_(
" -v, --verbose verbose mode\n"));
700 printf(
_(
" -V, --version output version information, then exit\n"));
701 printf(
_(
" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
702 printf(
_(
" -?, --help show this help, then exit\n"));
703 printf(
_(
"\nOptions controlling the output content:\n"));
704 printf(
_(
" -a, --data-only dump only the data, not the schema or statistics\n"));
705 printf(
_(
" -c, --clean clean (drop) databases before recreating\n"));
706 printf(
_(
" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
707 printf(
_(
" -g, --globals-only dump only global objects, no databases\n"));
708 printf(
_(
" -O, --no-owner skip restoration of object ownership\n"));
709 printf(
_(
" -r, --roles-only dump only roles, no databases or tablespaces\n"));
710 printf(
_(
" -s, --schema-only dump only the schema, no data or statistics\n"));
711 printf(
_(
" -S, --superuser=NAME superuser user name to use in the dump\n"));
712 printf(
_(
" -t, --tablespaces-only dump only tablespaces, no databases or roles\n"));
713 printf(
_(
" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
714 printf(
_(
" --binary-upgrade for use by upgrade utilities only\n"));
715 printf(
_(
" --column-inserts dump data as INSERT commands with column names\n"));
716 printf(
_(
" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
717 printf(
_(
" --disable-triggers disable triggers during data-only restore\n"));
718 printf(
_(
" --exclude-database=PATTERN exclude databases whose name matches PATTERN\n"));
719 printf(
_(
" --extra-float-digits=NUM override default setting for extra_float_digits\n"));
720 printf(
_(
" --filter=FILENAME exclude databases based on expressions in FILENAME\n"));
721 printf(
_(
" --if-exists use IF EXISTS when dropping objects\n"));
722 printf(
_(
" --inserts dump data as INSERT commands, rather than COPY\n"));
723 printf(
_(
" --load-via-partition-root load partitions via the root table\n"));
724 printf(
_(
" --no-comments do not dump comment commands\n"));
725 printf(
_(
" --no-data do not dump data\n"));
726 printf(
_(
" --no-policies do not dump row security policies\n"));
727 printf(
_(
" --no-publications do not dump publications\n"));
728 printf(
_(
" --no-role-passwords do not dump passwords for roles\n"));
729 printf(
_(
" --no-schema do not dump schema\n"));
730 printf(
_(
" --no-security-labels do not dump security label assignments\n"));
731 printf(
_(
" --no-statistics do not dump statistics\n"));
732 printf(
_(
" --no-subscriptions do not dump subscriptions\n"));
733 printf(
_(
" --no-sync do not wait for changes to be written safely to disk\n"));
734 printf(
_(
" --no-table-access-method do not dump table access methods\n"));
735 printf(
_(
" --no-tablespaces do not dump tablespace assignments\n"));
736 printf(
_(
" --no-toast-compression do not dump TOAST compression methods\n"));
737 printf(
_(
" --no-unlogged-table-data do not dump unlogged table data\n"));
738 printf(
_(
" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
739 printf(
_(
" --quote-all-identifiers quote all identifiers, even if not key words\n"));
740 printf(
_(
" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n"));
741 printf(
_(
" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n"));
742 printf(
_(
" --sequence-data include sequence data in dump\n"));
743 printf(
_(
" --statistics dump the statistics\n"));
744 printf(
_(
" --statistics-only dump only the statistics, not schema or data\n"));
745 printf(
_(
" --use-set-session-authorization\n"
746 " use SET SESSION AUTHORIZATION commands instead of\n"
747 " ALTER OWNER commands to set ownership\n"));
749 printf(
_(
"\nConnection options:\n"));
750 printf(
_(
" -d, --dbname=CONNSTR connect using connection string\n"));
751 printf(
_(
" -h, --host=HOSTNAME database server host or socket directory\n"));
752 printf(
_(
" -l, --database=DBNAME alternative default database\n"));
753 printf(
_(
" -p, --port=PORT database server port number\n"));
754 printf(
_(
" -U, --username=NAME connect as specified database user\n"));
755 printf(
_(
" -w, --no-password never prompt for password\n"));
756 printf(
_(
" -W, --password force password prompt (should happen automatically)\n"));
757 printf(
_(
" --role=ROLENAME do SET ROLE before dump\n"));
759 printf(
_(
"\nIf -f/--file is not used, then the SQL script will be written to the standard\n"
761 printf(
_(
"Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
762 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
781 "WHERE rolname !~ '^pg_' "
798 const char *rolename;
838 * Notes: rolconfig is dumped later, and pg_authid must be used for
839 * extracting rolcomment regardless of role_catalog.
843 "SELECT oid, rolname, rolsuper, rolinherit, "
844 "rolcreaterole, rolcreatedb, "
845 "rolcanlogin, rolconnlimit, rolpassword, "
846 "rolvaliduntil, rolreplication, rolbypassrls, "
847 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
848 "rolname = current_user AS is_current_user "
850 "WHERE rolname !~ '^pg_' "
854 "SELECT oid, rolname, rolsuper, rolinherit, "
855 "rolcreaterole, rolcreatedb, "
856 "rolcanlogin, rolconnlimit, rolpassword, "
857 "rolvaliduntil, rolreplication, rolbypassrls, "
858 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
859 "rolname = current_user AS is_current_user "
864 "SELECT oid, rolname, rolsuper, rolinherit, "
865 "rolcreaterole, rolcreatedb, "
866 "rolcanlogin, rolconnlimit, rolpassword, "
867 "rolvaliduntil, rolreplication, "
868 "false as rolbypassrls, "
869 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
870 "rolname = current_user AS is_current_user "
879 i_rolinherit =
PQfnumber(res,
"rolinherit");
880 i_rolcreaterole =
PQfnumber(res,
"rolcreaterole");
881 i_rolcreatedb =
PQfnumber(res,
"rolcreatedb");
882 i_rolcanlogin =
PQfnumber(res,
"rolcanlogin");
883 i_rolconnlimit =
PQfnumber(res,
"rolconnlimit");
884 i_rolpassword =
PQfnumber(res,
"rolpassword");
885 i_rolvaliduntil =
PQfnumber(res,
"rolvaliduntil");
886 i_rolreplication =
PQfnumber(res,
"rolreplication");
887 i_rolbypassrls =
PQfnumber(res,
"rolbypassrls");
888 i_rolcomment =
PQfnumber(res,
"rolcomment");
889 i_is_current_user =
PQfnumber(res,
"is_current_user");
896 const char *rolename;
902 if (strncmp(rolename,
"pg_", 3) == 0)
915 "SELECT pg_catalog.binary_upgrade_set_next_pg_authid_oid('%u'::pg_catalog.oid);\n\n",
920 * We dump CREATE ROLE followed by ALTER ROLE to ensure that the role
921 * will acquire the right properties even if it already exists (ie, it
922 * won't hurt for the CREATE to fail). This is particularly important
923 * for the role we are connected as, since even with --clean we will
924 * have failed to drop it. binary_upgrade cannot generate any errors,
925 * so we assume the current role is already created.
928 strcmp(
PQgetvalue(res,
i, i_is_current_user),
"f") == 0)
932 if (strcmp(
PQgetvalue(res,
i, i_rolsuper),
"t") == 0)
937 if (strcmp(
PQgetvalue(res,
i, i_rolinherit),
"t") == 0)
942 if (strcmp(
PQgetvalue(res,
i, i_rolcreaterole),
"t") == 0)
947 if (strcmp(
PQgetvalue(res,
i, i_rolcreatedb),
"t") == 0)
952 if (strcmp(
PQgetvalue(res,
i, i_rolcanlogin),
"t") == 0)
957 if (strcmp(
PQgetvalue(res,
i, i_rolreplication),
"t") == 0)
962 if (strcmp(
PQgetvalue(res,
i, i_rolbypassrls),
"t") == 0)
967 if (strcmp(
PQgetvalue(res,
i, i_rolconnlimit),
"-1") != 0)
1000 * Dump configuration settings for roles after all roles have been dumped.
1001 * We do it this way because config settings for roles could mention the
1002 * names of other roles.
1005 fprintf(
OPF,
"\n--\n-- User Configurations\n--\n");
1019 * Dump role memberships.
1021 * Note: we expect dumpRoles already created all the roles, but there is
1022 * no membership yet.
1034 bool dump_grant_options;
1042 int i_inherit_option;
1046 * Previous versions of PostgreSQL didn't used to track the grantor very
1047 * carefully in the backend, and the grantor could be any user even if
1048 * they didn't have ADMIN OPTION on the role, or a user that no longer
1049 * existed. To avoid dump and restore failures, don't dump the grantor
1050 * when talking to an old server version.
1052 * Also, in older versions the roleid and/or member could be role OIDs
1053 * that no longer exist. If we find such cases, print a warning and skip
1059 * Previous versions of PostgreSQL also did not have grant-level options.
1063 /* Generate and execute query. */
1065 "um.rolname AS member, "
1066 "ug.rolname AS grantor, "
1067 "a.roleid AS roleid, "
1068 "a.member AS memberid, "
1069 "a.grantor AS grantorid, "
1071 if (dump_grant_options)
1074 "LEFT JOIN %s ur on ur.oid = a.roleid "
1075 "LEFT JOIN %s um on um.oid = a.member "
1076 "LEFT JOIN %s ug on ug.oid = a.grantor "
1077 "WHERE NOT (ur.rolname ~ '^pg_' AND um.rolname ~ '^pg_')"
1084 i_memberid =
PQfnumber(res,
"memberid");
1085 i_grantorid =
PQfnumber(res,
"grantorid");
1086 i_admin_option =
PQfnumber(res,
"admin_option");
1087 i_inherit_option =
PQfnumber(res,
"inherit_option");
1088 i_set_option =
PQfnumber(res,
"set_option");
1091 fprintf(
OPF,
"--\n-- Role memberships\n--\n\n");
1094 * We can't dump these GRANT commands in arbitrary order, because a role
1095 * that is named as a grantor must already have ADMIN OPTION on the role
1096 * for which it is granting permissions, except for the bootstrap
1097 * superuser, who can always be named as the grantor.
1099 * We handle this by considering these grants role by role. For each role,
1100 * we initially consider the only allowable grantor to be the bootstrap
1101 * superuser. Every time we grant ADMIN OPTION on the role to some user,
1102 * that user also becomes an allowable grantor. We make repeated passes
1103 * over the grants for the role, each time dumping those whose grantors
1104 * are allowable and which we haven't done yet. Eventually this should let
1105 * us dump all the grants.
1108 while (
start < total)
1114 int prev_remaining = 0;
1117 /* If we hit a null roleid, we're done (nulls sort to the end). */
1120 /* translator: %s represents a numeric role OID */
1121 pg_log_warning(
"found orphaned pg_auth_members entry for role %s",
1126 /* All memberships for a single role should be adjacent. */
1127 for (end =
start; end < total; ++end)
1132 if (strcmp(role, otherrole) != 0)
1141 * Make repeated passes over the grants for this role until all have
1147 * We should make progress on every iteration, because a notional
1148 * graph whose vertices are grants and whose edges point from
1149 * grantors to members should be connected and acyclic. If we fail
1150 * to make progress, either we or the server have messed up.
1154 pg_log_error(
"could not find a legal dump ordering for memberships in role \"%s\"",
1161 /* Make one pass over the grants for this role. */
1171 /* If we already did this grant, don't do it again. */
1175 /* Complain about, then ignore, entries with orphaned OIDs. */
1178 /* translator: %s represents a numeric role OID */
1179 pg_log_warning(
"found orphaned pg_auth_members entry for role %s",
1187 /* translator: %s represents a numeric role OID */
1188 pg_log_warning(
"found orphaned pg_auth_members entry for role %s",
1199 if (dump_grant_options)
1203 * If we're not dumping grantors or if the grantor is the
1204 * bootstrap superuser, it's fine to dump this now. Otherwise,
1205 * it's got to be someone who has already been granted ADMIN
1208 if (dump_grantors &&
1209 atooid(grantorid) != BOOTSTRAP_SUPERUSERID &&
1210 rolename_lookup(ht, grantor) == NULL)
1213 /* Remember that we did this so that we don't do it again. */
1218 * If ADMIN OPTION is being granted, remember that grants
1219 * listing this member as the grantor can now be dumped.
1222 rolename_insert(ht, member, &found);
1224 /* Generate the actual GRANT statement. */
1230 if (dump_grant_options)
1234 if (optbuf->
data[0] !=
'0円')
1243 if (optbuf->
data[0] !=
'0円')
1247 if (optbuf->
data[0] !=
'0円')
1255 rolename_destroy(ht);
1268 * Dump role configuration parameter privileges. This code is used for 15.0
1269 * and later servers.
1271 * Note: we expect dumpRoles already created all the roles, but there are
1272 * no per-role configuration parameter privileges yet.
1281 * Get all parameters that have non-default acls defined.
1284 "pg_catalog.pg_get_userbyid(" CppAsString2(BOOTSTRAP_SUPERUSERID)
") AS parowner, "
1286 "pg_catalog.acldefault('p', " CppAsString2(BOOTSTRAP_SUPERUSERID)
") AS acldefault "
1287 "FROM pg_catalog.pg_parameter_acl "
1291 fprintf(
OPF,
"--\n-- Role privileges on configuration parameters\n--\n\n");
1302 /* needed for buildACLCommands() */
1309 pg_log_error(
"could not parse ACL list (%s) for parameter \"%s\"",
1336 * Get all tablespaces except built-in ones (which we assume are named
1340 "FROM pg_catalog.pg_tablespace "
1341 "WHERE spcname !~ '^pg_' "
1345 fprintf(
OPF,
"--\n-- Drop tablespaces\n--\n\n");
1371 * Get all tablespaces except built-in ones (which we assume are named
1375 "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1376 "pg_catalog.pg_tablespace_location(oid), "
1377 "spcacl, acldefault('t', spcowner) AS acldefault, "
1378 "array_to_string(spcoptions, ', '),"
1379 "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1380 "FROM pg_catalog.pg_tablespace "
1381 "WHERE spcname !~ '^pg_' "
1400 /* needed for buildACLCommands() */
1406 appendPQExpBuffer(
buf,
"SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid);\n", spcoid);
1415 * In-place tablespaces use a relative path, and need to be dumped
1416 * with an empty string as location.
1425 if (spcoptions && spcoptions[0] !=
'0円')
1427 fspcname, spcoptions);
1429 /* tablespaces can't have initprivs */
1436 pg_log_error(
"could not parse ACL list (%s) for tablespace \"%s\"",
1442 if (!
no_comments && spccomment && spccomment[0] !=
'0円')
1451 "TABLESPACE", spcname,
1466 * Dump commands to drop each database.
1475 * Skip databases marked not datallowconn, since we'd be unable to connect
1476 * to them anyway. This must agree with dumpDatabases().
1480 "FROM pg_database d "
1481 "WHERE datallowconn AND datconnlimit != -2 "
1482 "ORDER BY datname");
1485 fprintf(
OPF,
"--\n-- Drop databases (except postgres and template1)\n--\n\n");
1492 * Skip "postgres" and "template1"; dumpDatabases() will deal with
1493 * them specially. Also, be sure to skip "template0", even if for
1494 * some reason it's not marked !datallowconn.
1496 if (strcmp(
dbname,
"template1") != 0 &&
1497 strcmp(
dbname,
"template0") != 0 &&
1498 strcmp(
dbname,
"postgres") != 0)
1513 * Dump user-specific configuration
1522 "WHERE setdatabase = 0 AND setrole = "
1523 "(SELECT oid FROM %s WHERE rolname = ",
1535 fprintf(
OPF,
"\n--\n-- User Config \"%s\"\n--\n\n", sanitized);
1554 * Find a list of database names that match the given patterns.
1555 * See also expand_table_name_patterns() in pg_dump.c
1565 if (patterns->
head == NULL)
1566 return;
/* nothing to do */
1571 * The loop below runs multiple SELECTs, which might sometimes result in
1572 * duplicate entries in the name list, but we don't care, since all we're
1573 * going to do is test membership of the list.
1581 "SELECT datname FROM pg_catalog.pg_database n\n");
1583 false, NULL,
"datname", NULL, NULL, NULL,
1588 pg_log_error(
"improper qualified name (too many dotted names): %s",
1608 * Dump contents of databases.
1617 * Skip databases marked not datallowconn, since we'd be unable to connect
1618 * to them anyway. This must agree with dropDBs().
1620 * We arrange for template1 to be processed first, then we process other
1621 * DBs in alphabetical order. If we just did them all alphabetically, we
1622 * might find ourselves trying to drop the "postgres" database while still
1623 * connected to it. This makes trying to run the restore script while
1624 * connected to "template1" a bad idea, but there's no fixed order that
1625 * doesn't have some failure mode with --clean.
1629 "FROM pg_database d "
1630 "WHERE datallowconn AND datconnlimit != -2 "
1631 "ORDER BY (datname <> 'template1'), datname");
1640 const char *create_opts;
1643 /* Skip template0, even if it's not marked !datallowconn. */
1644 if (strcmp(
dbname,
"template0") == 0)
1647 /* Skip any explicitly excluded database */
1657 fprintf(
OPF,
"--\n-- Database \"%s\" dump\n--\n\n", sanitized);
1661 * We assume that "template1" and "postgres" already exist in the
1662 * target installation. dropDBs() won't have removed them, for fear
1663 * of removing the DB the restore script is initially connected to. If
1664 * --clean was specified, tell pg_dump to drop and recreate them;
1665 * otherwise we'll merely restore their contents. Other databases
1666 * should simply be created.
1668 if (strcmp(
dbname,
"template1") == 0 || strcmp(
dbname,
"postgres") == 0)
1671 create_opts =
"--clean --create";
1675 /* Since pg_dump won't emit a \connect command, we must */
1680 create_opts =
"--create";
1693 pg_fatal(
"could not re-open the output file \"%s\": %m",
1704 * Run pg_dump on dbname, with specified options.
1720 * If we have a filename, use the undocumented plain-append pg_dump
1729 * Append the database name to the already-constructed stem of connection
1741 ret = system(cmd.
data);
1752 * Build SECURITY LABEL command(s) for a shared object
1754 * The caller has to provide object type and identity in two separate formats:
1755 * catalog_name (e.g., "pg_database") and object OID, as well as
1756 * type name (e.g., "DATABASE") and object name (not pre-quoted).
1758 * The command(s) are appended to "buffer".
1762 const char *objtype,
const char *objname,
1777 * As above for a SQL command (which returns nothing).
1807 time_t
now = time(NULL);
1814 * read_dumpall_filters - retrieve database identifier patterns from file
1816 * Parse the specified filter file for include and exclude patterns, and add
1817 * them to the relevant lists. If the filename is "-" then filters will be
1818 * read from STDIN rather than a file.
1820 * At the moment, the only allowed filter is for database exclusion.
Acl * acldefault(ObjectType objtype, Oid ownerId)
Datum now(PG_FUNCTION_ARGS)
#define PG_TEXTDOMAIN(domain)
int find_my_exec(const char *argv0, char *retpath)
void set_pglocale_pgservice(const char *argv0, const char *app)
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
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)
#define fprintf(file, fmt, msg)
char * generate_restrict_key(void)
bool buildACLCommands(const char *name, const char *subname, const char *nspname, const char *type, const char *acls, const char *baseacls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
bool valid_restrict_key(const char *restrict_key)
void buildShSecLabelQuery(const char *catalog_name, Oid objectId, PQExpBuffer sql)
void makeAlterConfigCommand(PGconn *conn, const char *configitem, const char *type, const char *name, const char *type2, const char *name2, PQExpBuffer buf)
char * sanitize_line(const char *str, bool want_hyphen)
void emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *objtype, const char *objname)
#define PGDUMP_STRFTIME_FMT
void fsync_fname(const char *fname, bool isdir)
int PQserverVersion(const PGconn *conn)
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
int PQclientEncoding(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
int PQsetClientEncoding(PGconn *conn, const char *encoding)
int PQfnumber(const PGresult *res, const char *field_name)
PGresult * PQexec(PGconn *conn, const char *query)
char * pg_strdup(const char *in)
void * pg_malloc0(size_t size)
void filter_init(FilterStateData *fstate, const char *filename, exit_function f_exit)
void filter_free(FilterStateData *fstate)
const char * filter_object_type_name(FilterObjectType fot)
bool filter_read_item(FilterStateData *fstate, char **objname, FilterCommandType *comtype, FilterObjectType *objtype)
void pg_log_filter_error(FilterStateData *fstate, const char *fmt,...)
@ FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN
@ FILTER_OBJECT_TYPE_SCHEMA
@ FILTER_OBJECT_TYPE_INDEX
@ FILTER_OBJECT_TYPE_TRIGGER
@ FILTER_OBJECT_TYPE_FOREIGN_DATA
@ FILTER_OBJECT_TYPE_DATABASE
@ FILTER_OBJECT_TYPE_FUNCTION
@ FILTER_OBJECT_TYPE_TABLE_DATA
@ FILTER_OBJECT_TYPE_NONE
@ FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN
@ FILTER_OBJECT_TYPE_EXTENSION
@ FILTER_OBJECT_TYPE_TABLE
@ FILTER_COMMAND_TYPE_INCLUDE
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
void pg_logging_increase_verbosity(void)
void pg_logging_init(const char *argv0)
void pg_logging_set_level(enum pg_log_level new_level)
#define pg_log_error(...)
#define pg_log_error_hint(...)
#define pg_log_error_detail(...)
void exit_nicely(int code)
static void dumpTimestamp(const char *msg)
static int on_conflict_do_nothing
int main(int argc, char *argv[])
static void dropTablespaces(PGconn *conn)
static void expand_dbname_patterns(PGconn *conn, SimpleStringList *patterns, SimpleStringList *names)
static int no_role_passwords
static PQExpBuffer pgdumpopts
static int statistics_only
static int no_table_access_method
static int no_unlogged_table_data
static int binary_upgrade
static int disable_triggers
static void dumpUserConfig(PGconn *conn, const char *username)
static const char * connstr
static SimpleStringList database_exclude_patterns
static void dumpTablespaces(PGconn *conn)
static void dumpRoleMembership(PGconn *conn)
static char pg_dump_bin[MAXPGPATH]
static SimpleStringList database_exclude_names
static int no_publications
static char * restrict_key
static int no_security_labels
static void dumpDatabases(PGconn *conn)
static int disable_dollar_quoting
static void buildShSecLabels(PGconn *conn, const char *catalog_name, Oid objectId, const char *objtype, const char *objname, PQExpBuffer buffer)
static int no_tablespaces
static int no_toast_compression
static void executeCommand(PGconn *conn, const char *query)
static int runPgDump(const char *dbname, const char *create_opts)
static void dumpRoleGUCPrivs(PGconn *conn)
static void dumpRoles(PGconn *conn)
static int use_setsessauth
static int load_via_partition_root
static int column_inserts
static void read_dumpall_filters(const char *filename, SimpleStringList *pattern)
static void dropRoles(PGconn *conn)
static void dropDBs(PGconn *conn)
static int server_version
static char role_catalog[10]
static int no_subscriptions
static int with_statistics
#define PGDUMP_VERSIONSTR
PGDLLIMPORT char * optarg
#define pg_encoding_to_char
static const char * pghost
static const char * pgport
#define pg_log_warning(...)
#define is_absolute_path(filename)
const char * get_progname(const char *argv0)
size_t strlcpy(char *dst, const char *src, size_t siz)
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
PQExpBuffer createPQExpBuffer(void)
void initPQExpBuffer(PQExpBuffer str)
void resetPQExpBuffer(PQExpBuffer str)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void destroyPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void termPQExpBuffer(PQExpBuffer str)
bool quote_all_identifiers
bool simple_string_list_member(SimpleStringList *list, const char *val)
void simple_string_list_append(SimpleStringList *list, const char *val)
const char * fmtId(const char *rawid)
void setFmtEncoding(int encoding)
void appendShellString(PQExpBuffer buf, const char *str)
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
bool processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern, bool have_where, bool force_escape, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule, PQExpBuffer dbnamebuf, int *dotcnt)
void appendConnStrVal(PQExpBuffer buf, const char *str)
struct SimpleStringListCell * next
SimpleStringListCell * head