PostgreSQL already comes with plenty of system information functions to reconstruct the commands to create various objects, e.g. constraints or indexes. Starting with PostgreSQL 19 more functions will be available, namely those:
- pg_get_database_ddl
- pg_get_role_ddl
- pg_get_tablespace_ddl
As the names imply they can be used to recreate the commands to create a database, a role, or a tablespace.
To see what they do lets create a small setup:
postgres=# select version();
version
---------------------------------------------------------------------------------------
PostgreSQL 19devel dbi services build on x86_64-linux, compiled by gcc-15.1.1, 64-bit
(1 row)
postgres=# create user u with login password 'u';
CREATE ROLE
postgres=# \! mkdir /var/tmp/tbs
postgres=# create tablespace tbs location '/var/tmp/tbs' with ( random_page_cost = 1.1 );
CREATE TABLESPACE
postgres=# create database d with owner = u tablespace = tbs;
CREATE DATABASE
postgres=# alter database d connection limit = 10;
ALTER DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+-------------+-----------+-----------------------
d | u | UTF8 | icu | en_US.UTF-8 | en_US.UTF-8 | en-US-x-icu | |
postgres | postgres | UTF8 | icu | en_US.UTF-8 | en_US.UTF-8 | en-US-x-icu | |
template0 | postgres | UTF8 | icu | en_US.UTF-8 | en_US.UTF-8 | en-US-x-icu | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | icu | en_US.UTF-8 | en_US.UTF-8 | en-US-x-icu | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(4 rows)
To get the commands to recreate that database the new function “pg_get_database_ddl” can be used:
postgres=# select * from pg_get_database_ddl ( 'd'::regdatabase );
pg_get_database_ddl
---------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE DATABASE d WITH TEMPLATE = template0 ENCODING = 'UTF8' LOCALE_PROVIDER = icu LOCALE = 'en_US.UTF-8' ICU_LOCALE = 'en-US-x-icu' TABLESPACE = tbs;
ALTER DATABASE d OWNER TO u;
ALTER DATABASE d CONNECTION LIMIT = 10;
(3 rows)
There are some options to control the output format and what gets reconstructed, e.g.:
postgres=# select * from pg_get_database_ddl ( 'd'::regdatabase, 'pretty', 'true' );
pg_get_database_ddl
-----------------------------------------
CREATE DATABASE d +
WITH TEMPLATE = template0 +
ENCODING = 'UTF8' +
LOCALE_PROVIDER = icu +
LOCALE = 'en_US.UTF-8' +
ICU_LOCALE = 'en-US-x-icu' +
TABLESPACE = tbs;
ALTER DATABASE d OWNER TO u;
ALTER DATABASE d CONNECTION LIMIT = 10;
(3 rows)
postgres=# select * from pg_get_database_ddl ( 'd'::regdatabase, 'pretty', 'true', 'owner', 'false' );
pg_get_database_ddl
-----------------------------------------
CREATE DATABASE d +
WITH TEMPLATE = template0 +
ENCODING = 'UTF8' +
LOCALE_PROVIDER = icu +
LOCALE = 'en_US.UTF-8' +
ICU_LOCALE = 'en-US-x-icu' +
TABLESPACE = tbs;
ALTER DATABASE d CONNECTION LIMIT = 10;
(2 rows)
postgres=# select * from pg_get_database_ddl ( 'd'::regdatabase, 'pretty', 'true', 'owner', 'false', 'tablespace', 'false' );
pg_get_database_ddl
-----------------------------------------
CREATE DATABASE d +
WITH TEMPLATE = template0 +
ENCODING = 'UTF8' +
LOCALE_PROVIDER = icu +
LOCALE = 'en_US.UTF-8' +
ICU_LOCALE = 'en-US-x-icu';
ALTER DATABASE d CONNECTION LIMIT = 10;
(2 rows)
The other two functions behave the same (but do not have exactly the same options):
postgres=# select * from pg_get_tablespace_ddl('tbs');
pg_get_tablespace_ddl
---------------------------------------------------------------
CREATE TABLESPACE tbs OWNER postgres LOCATION '/var/tmp/tbs';
ALTER TABLESPACE tbs SET (random_page_cost='1.1');
(2 rows)
postgres=# select * from pg_get_tablespace_ddl('tbs', 'pretty', 'true');
pg_get_tablespace_ddl
----------------------------------------------------
CREATE TABLESPACE tbs +
OWNER postgres +
LOCATION '/var/tmp/tbs';
ALTER TABLESPACE tbs SET (random_page_cost='1.1');
(2 rows)
postgres=# select * from pg_get_tablespace_ddl('tbs', 'pretty', 'true', 'owner', 'false');
pg_get_tablespace_ddl
----------------------------------------------------
CREATE TABLESPACE tbs +
LOCATION '/var/tmp/tbs';
ALTER TABLESPACE tbs SET (random_page_cost='1.1');
(2 rows)
… and finally for the roles:
postgres=# select * from pg_get_role_ddl ('u');
pg_get_role_ddl
--------------------------------------------------------------------------------------------
CREATE ROLE u NOSUPERUSER INHERIT NOCREATEROLE NOCREATEDB LOGIN NOREPLICATION NOBYPASSRLS;
(1 row)
postgres=# select * from pg_get_role_ddl ('u', 'pretty', 'true');
pg_get_role_ddl
-------------------
CREATE ROLE u +
NOSUPERUSER +
INHERIT +
NOCREATEROLE +
NOCREATEDB +
LOGIN +
NOREPLICATION+
NOBYPASSRLS;
(1 row)
postgres=# select * from pg_get_role_ddl ('u', 'pretty', 'true', 'memberships', 'false');
pg_get_role_ddl
-------------------
CREATE ROLE u +
NOSUPERUSER +
INHERIT +
NOCREATEROLE +
NOCREATEDB +
LOGIN +
NOREPLICATION+
NOBYPASSRLS;
(1 row)
Nice, and again: Thanks to all involved.