Skip to content

Add pg_get_database_ddl() function to reconstruct CREATE DATABASE statements.#4

Open
akshay-joshi wants to merge 2 commits intomasterfrom
get_database_ddl
Open

Add pg_get_database_ddl() function to reconstruct CREATE DATABASE statements.#4
akshay-joshi wants to merge 2 commits intomasterfrom
get_database_ddl

Conversation

@akshay-joshi
Copy link
Owner

This adds a new system function, pg_get_database_ddl(database_name/database_oid, pretty), which reconstructs the CREATE DATABASE statement for a given database name or database oid.

Usage:
SELECT pg_get_database_ddl('postgres'); // Non pretty-formatted DDL
SELECT pg_get_database_ddl(16835); // Non pretty-formatted DDL
SELECT pg_get_database_ddl('postgres', true); // pretty-formatted DDL
SELECT pg_get_database_ddl(16835, true); // pretty-formatted DDL

Reference: PG-150
Author: Akshay Joshi akshay.joshi@enterprisedb.com
Reviewed-by: Álvaro Herrera alvherre@kurilemu.de

@akshay-joshi akshay-joshi force-pushed the get_database_ddl branch 9 times, most recently from 25bd4ed to cfbbcd4 Compare November 17, 2025 07:44
@akshay-joshi akshay-joshi force-pushed the get_database_ddl branch 5 times, most recently from 76b4776 to 8ea45bf Compare November 20, 2025 08:37
@akshay-joshi akshay-joshi force-pushed the get_database_ddl branch 6 times, most recently from 7042470 to ece82b3 Compare January 16, 2026 07:18
@akshay-joshi akshay-joshi force-pushed the get_database_ddl branch 2 times, most recently from 42e3705 to 77b9c4d Compare January 21, 2026 08:02
…tements.

This adds a new system function, pg_get_database_ddl(database_name/database_oid, ddl_options),
which reconstructs the CREATE DATABASE statement for a given database name or database oid.
Supported ddl_options are 'pretty', '--no-owner', '--no-tablespace' and '--with-defaults'.

Usage:
  SELECT pg_get_database_ddl('postgres'); // Non pretty-formatted DDL
  SELECT pg_get_database_ddl(16835); // Non pretty-formatted DDL
  SELECT pg_get_database_ddl('postgres', 'pretty'); // pretty-formatted DDL
  SELECT pg_get_database_ddl('postgres', '--no-owner', '--no-tablespace'); // Omits the Owner and Tablespace clause from the DDL.
  SELECT pg_get_database_ddl('postgres', 'pretty', '--with-defaults'); // Includes clauses for parameters that are currently at their default values.

Reference: PG-150
Author: Akshay Joshi <akshay.joshi@enterprisedb.com>
Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de>
Reviewed-by: Quan Zongliang <quanzongliang@yeah.net>
Reviewed-by: Japin Li <japinli@hotmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Euler Taveira <euler@eulerto.com>
…tements.

This adds a new system function, pg_get_database_ddl(database_name/database_oid, ddl_options),
which reconstructs the CREATE DATABASE statement for a given database name or database oid.
Supported ddl_options are 'pretty', 'owner=no/false/0', 'tablespace=no/false/0' and 'defaults'.

Usage:
  SELECT pg_get_database_ddl('postgres'); // Non pretty-formatted DDL
  SELECT pg_get_database_ddl(16835); // Non pretty-formatted DDL
  SELECT pg_get_database_ddl('postgres', 'pretty'); // pretty-formatted DDL
  SELECT pg_get_database_ddl('postgres', 'owner=no', 'tablespace=false'); -- Omits Owner and Tablespace clauses.
  SELECT pg_get_database_ddl('postgres', 'pretty', 'defaults=yes'); -- Includes clauses for parameters at their default values.
  SELECT pg_get_database_ddl('postgres', 'pretty', 'defaults'); -- Includes clauses for parameters at their default values.

Reference: PG-150
Author: Akshay Joshi <akshay.joshi@enterprisedb.com>
Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de>
Reviewed-by: Quan Zongliang <quanzongliang@yeah.net>
Reviewed-by: Japin Li <japinli@hotmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Euler Taveira <euler@eulerto.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments