From be13baa1ff26ac7f2175e4b149a393d430fc5a7f Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" Date: Wed, 17 Jun 2026 13:51:42 +0800 Subject: [PATCH v1] Fix pg_restore --no-globals SQL output for pg_dumpall archives Commit 3c19983cc08 allowed pg_restore to restore pg_dumpall archives with --no-globals but without -C. That is valid for a direct restore, where pg_restore can connect to each existing database by name, but it is not valid when writing a SQL script. Without -C, DATABASE and DATABASE PROPERTIES TOC entries are skipped. For SQL output, that means the generated script has no CREATE DATABASE or reconnect commands to separate objects from different databases, so objects from multiple source databases can be restored into one target database. Require -C for pg_dumpall archive restores unless restoring globals only, or unless --no-globals is used in direct restore mode. Add a test covering the broken --no-globals SQL-output case. Author: Chao Li --- src/bin/pg_dump/pg_restore.c | 9 ++++++--- src/bin/pg_dump/t/007_pg_dumpall.pl | 12 ++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 48fdcb0fae1..648bf64a461 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -575,10 +575,13 @@ main(int argc, char **argv) /* * To restore from a pg_dumpall archive, -C (create database) option - * must be specified unless we are only restoring globals or we are - * skipping globals. + * must be specified unless we are only restoring globals, or we are + * skipping globals in a direct restore. When writing a SQL script, + * -C is still required because the script needs CREATE DATABASE and + * reconnect commands to separate each database's objects. */ - if (!no_globals && !globals_only && opts->createDB != 1) + if (!globals_only && opts->createDB != 1 && + (!no_globals || !opts->useDB)) { pg_log_error("option %s must be specified when restoring an archive created by pg_dumpall", "-C/--create"); diff --git a/src/bin/pg_dump/t/007_pg_dumpall.pl b/src/bin/pg_dump/t/007_pg_dumpall.pl index 22f11a13a9a..93355f4c5b0 100644 --- a/src/bin/pg_dump/t/007_pg_dumpall.pl +++ b/src/bin/pg_dump/t/007_pg_dumpall.pl @@ -387,6 +387,18 @@ $node->command_fails_like( qr/\Qpg_restore: error: option -C\/--create must be specified when restoring an archive created by pg_dumpall\E/, 'When -C is not used in pg_restore with dump of pg_dumpall'); +# report an error when -C is not used for SQL output, even with --no-globals +$node->command_fails_like( + [ + 'pg_restore', + "$tempdir/format_custom", + '--format' => 'custom', + '--no-globals', + '--file' => "$tempdir/error_test.sql", + ], + qr/\Qpg_restore: error: option -C\/--create must be specified when restoring an archive created by pg_dumpall\E/, + 'When -C is not used with --no-globals and --file in pg_restore with dump of pg_dumpall'); + # report an error when \l/--list option is used with dump of pg_dumpall $node->command_fails_like( [ -- 2.50.1 (Apple Git-155)