Skip to content

6 Utilities

Changes to configure, initdb, backup, restore, upgrade, psql, and maintenance tools.
GASource pages: PDF 74-88Original PDF on GitHub

PDF page 74

Describes the major enhancements of utility commands.

3.4.1 Configure

The configure command has the following options changed. [9db49fc, d3b111e, fcb21b3]

Option nameModifyDescription
–with-gnu-ldRemoveAssume the C compiler uses GNU ld
–with-segsize-blocksAddSet table segment size in blocks
–with-icuRemoveBuild with ICU support
–without-icuAddBuild without ICU support
Table 28 Add/Remove options

3.4.2 Createuser

Multiple options have been added to the createuser command. The –role option can still be used, but it is deprecated. [08951a7, 2dcd157, 381d19b]

Option nameShort optionDescription
–with-admin=ROLE-aROLE will be a member of new role with admin option
–with-member=ROLE-mROLE will be a member of new role
–valid-until=TIMESTAMP-vPassword expiration
–bypassrls-Add Bypass RLS attribute
–no-bypassrls-Do not assign Bypass RLS attribute
–member-of=ROLE-gnew role will be a member of ROLE (modified from –role option)
Table 29 Add/Modify options
Example 84 Execute the createuser command
$ createuser --member=role1 user1 --echo
SELECT pg_catalog.set_config('search_path', '', false);
CREATE
ROLE
user1 NOSUPERUSER NOCREATEDB
NOCREATEROLE INHERIT
LOGIN
NOREPLICATION NOBYPASSRLS IN ROLE role1;

PDF page 75

3.4.3 Initdb

The following enhancements have been implemented to the initdb command: [27b6237, 30a53b7, 3e51b27, af3abca]

–icu-rules option

The –icu-rules option has been added to add a custom rule to the ICU locale.

Example 85 Execute initdb command with the --icu-rules option
$ initdb --icu-local=en_US --locale-provider=icu --icu-rules='&a < g' data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
Using language tag "en-US" for ICU locale "en_US".
The database cluster will be initialized with this locale configuration:
  provider:    icu
  ICU locale:  en_US
  LC_COLLATE:  en_US.UTF-8
...
–set option

The –set option (shorthand option is -c) specifies the initial values of the parameters described in the postgresql.conf file. This option can be specified multiple times.

Example 86 initdb command with the --set option
$ initdb --set port=5433 --set listen_addresses='*' --locale-provider=libc --
no-locale --encoding=utf8 data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".
Data page checksums are disabled.
…

PDF page 76

Check locale command

The initdb command now exits successfully even if the ’locale’ command does not exist. In previous versions, the initdb command would fail if the locale -a command failed.

3.4.4 Pgindent

The following options have been added/extended to the pgindent command. [b90f0b5, a1c4cd6, 068a243, b16259b]

Option nameChangeDescription
–buildRemoveBuilding the pg_bsd_indent command
–code-baseRemoveSpecifying base directory for source code
–commit=gitrefAddTarget files changed in the specified commit
–excludes=PATHChangeCan be specified multiple times
–helpAddOutput usage
–show-diffAddView changes
–silent-diffAddExit with return value 2 if there are any changes
Table 30 Add/change options

3.4.5 Pg_basebackup

It is now possible to specify ’long’ for the level of the Zstandard compression method. This option improves the compression ratio for large data, but increases resource usage. [2820adf]

Example 87 Specifying the compression level to &#x27;long&#x27;
$ pg_basebackup -D data.zstd --compress=zstd:long --format=tar
$ file data.zstd/*
data.zstd/backup_manifest: ASCII text
data.zstd/base.tar.zst:    Zstandard compressed data (v0.8+), Dictionary ID:
None
data.zstd/pg_wal.tar:      POSIX tar archive

PDF page 77

3.4.6 Pg_bsd_indent

The BSD-style indentation tool the pg_bsd_indent command, developed in a separate project, has been incorporated into the core repository. [4e831f4]

Example 88 Execute the pg_bsd_indent command
$ pg_bsd_indent -v indent.c
There were 1273 output lines and 223 comments
(Lines with comments)/(Lines with code):  0.548

3.4.7 Pg_dump

The following enhancements have been implemented in the pg_dump command. [5e73a60, 0da243f, 84adc8e, 2820adf, 35ce24c, 2f80c95, a563c24, 5f53b42]

–compress option

The –compress option now accepts a compression method and compression level separated by a colon (:). The compression method can be none, gzip, lz4 or zstd. The value that can be specified for compression level differs depending on the compression method.

Compression MethodDefault levelLevel RangeNote
none--
gzip-11~9
lz401~12
zstd3-131072~22, long
Table 31 Compression level range

PDF page 78

Example 89 Execute the pg_dump command
$ pg_dump --compress=gzip:9 --file=postgres.gz postgres
$ file postgres.gz
postgres.gz: gzip compressed data, max compression, from Unix, original size
11963
$ pg_dump --compress=lz4:12 --file=postgres.lz4 postgres
$ file postgres.lz4
postgres.lz4: LZ4 compressed data (v1.4+)
$ pg_dump --compress=zstd:22 --file=postgres.zstd postgres
$ file postgres.zstd
postgres.zstd: Zstandard compressed data (v0.8+), Dictionary ID: None
–large-objects option

The –blob and –no-blob options from previous versions have been changed to –large-objects and - -no-large-objects. Legacy options are still available, but are deprecated.

Example 90 Help message of the pg_dump command
$ pg_dump --help
pg_dump dumps a database as a text file or to other formats.
…
Options controlling the output content:
  -a, --data-only              dump only the data, not the schema
  -b, --large-objects          include large objects in dump
  --blobs                      (same as --large-objects, deprecated)
  -B, --no-large-objects       exclude large objects in dump
  --no-blobs                   (same as --no-large-objects, deprecated)
  -c, --clean                  (drop) database objects before recreating
…
–table-and-children option

Basically, the same as the –table option. Includes tables matching the pattern and their child tables and partitions.

PDF page 79

–exclude-table-and-children option

Basically, the same as the –exclude-table option. Exclude tables matching the pattern and their child tables and partitions.

–exclude-table-data-and-children option

Basically, the same as the –exclude-table-data option. Exclude data from tables matching the pattern and their child tables and partitions.

Lock acquisition

The pg_dump command executes a LOCK TABLE statement on the target table at startup. In PostgreSQL 16, multiple table names are described for one LOCK TABLE to reduce the amount of communication between the client and server.

3.4.8 Pg_receivewal / Pg_recvlogical

The SIGTERM signal has been added as a program exit condition in addition to the existing SIGINT signal. This change corresponds to the default value of the systemd exit signal (KillSignal). [8b60db7]

3.4.9 Pg_upgrade

Add the –copy option to the pg_upgrade command. This is the default behavior. Copies locale and encoding information from source to destination. [746915c, 9637bad]

3.4.10 Pg_verifybackup

Add the –progress option (shortened option -P). Report progress. [d07c294]

PDF page 80

Example 91 Execute the pg_verifybackup command
$ pg_basebackup -D back.1 -v
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/5E000028 on timeline 1
pg_basebackup: starting background WAL receiver
pg_basebackup: created temporary replication slot "pg_basebackup_120380"
pg_basebackup: write-ahead log end point: 0/5E0001B0
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: syncing data to disk ...
pg_basebackup: renaming backup_manifest.tmp to backup_manifest
pg_basebackup: base backup completed
$ pg_verifybackup back.1 --progress
887802/887802 kB (100%) verified
backup successfully verified

3.4.11 Pg_waldump

The following new features have been added to the pg_waldump command. [d497093, 4c8044c , b381d96]

–save-fullpage option

Add the –save-fullpage option to specify the directory to save full page images. The specified directory must be empty. A full-page image is created with the following file name:

<timeline>-<lsn>.<tablespace>.<database>.<relation>.<block>_<forkname>

PDF page 81

NotationDescriptionExample
Timeline hexadecimal format00000000
LSN hexadecimal format05AD6E48
OID of the tablespace1663
OID of the database5
Relation filenode16388
Block number20
Fork namemain
Table 32 File name explanation
Example 92 Execute the pg_waldump command with the --save-fullpage option
$ pg_waldump --save-fullpage=$HOME/fullpage
data/pg_wal/000000010000000000000004
rmgr: Heap        len (rec/tot):     65/    65, tx:        740, lsn: …
rmgr: Heap        len (rec/tot):     65/    65, tx:        740, lsn: …
…
$ ls -1 $HOME/fullpage
00000000-00000000-05A26608.1663.5.16388.0_vm
00000000-00000000-05A7D2E8.1663.5.2619.18_main
00000000-00000000-05A7E920.1663.5.2696.1_main
–timeline option

The –timeline option now accepts hexadecimal strings.

Example 93 Execute the pg_waldump command with the --timeline option
$ pg_waldump 000000010000000000000001  --timeline=0xA
rmgr: XLOG        len (rec/tot):    114/   114, tx:          0, lsn:
0/01000028, prev 0/00000000, desc: CHECKPOINT_SHUTDOWN redo 0/1000028; tli 1;
pr
ev tli 1; fpw true; xid 0:3; oid 10000; multi 1; offset 0; oldest xid 3 in DB
1; oldest multi 1 in DB 1; oldest/newest commit timestamp xid: 0/0; old
est running xid 0; shutdown
…

PDF page 82

3.4.12 Psql

The following enhancements have been implemented to the psql command: [b0d8f2d, 31ae2aa, bd95816, 5b66de3, a45388d, d913928, 6f9ee74, 31ae2aa]

Command execution status

The execution status of !, \g, \o, \w, and \copy metacommands can now be obtained with the following variables.

Variable nameDescription
SHELL_ERROR’true’ if command execution result is 0, ‘false’ if not 0
SHELL_EXIT_CODE Command exit codeCommandexitcode
Table 33 Command execution status
Example 94 Check command execution status
postgres=> \! ls postgresql.conf
postgresql.conf
postgres=> \echo :SHELL_ERROR
false
postgres=> \echo :SHELL_EXIT_CODE
0
postgres=> \! ls postgresql.conf.bad
ls: cannot access 'postgresql.conf.bad': No such file or directory
postgres=> \echo :SHELL_ERROR
true
postgres=> \echo :SHELL_EXIT_CODE
2
\d+ metacommand

Executing the \d+ meta command on a partitioned table will output “FOREIGN” if the partition is an external table.

PDF page 83

Example 95 External table partition information
postgres=> CREATE FOREIGN TABLE remote1(c1 INT, c2 VARCHAR(10)) SERVER remsvr1 ;
CREATE FOREIGN TABLE
postgres=> CREATE TABLE part1(c1 INT, c2 VARCHAR(10)) PARTITION BY RANGE(c1) ;
CREATE TABLE
postgres=> ALTER TABLE part1 ATTACH PARTITION remote1 FOR VALUES FROM (0) TO
(100) ;
ALTER TABLE
postgres=> \d+ part1
                                           Partitioned table "public.part1"
 Column |         Type          | Collation | Nullable | Default | Storage …
--------+-----------------------+-----------+----------+---------+-------- …
c1     | integer               |           |          |         | plain   …
 c2     | character varying(10) |           |          |         | extended…
Partition key: RANGE (c1)
Partitions: remote1 FOR VALUES FROM (0) TO (100), FOREIGN
\bind metacommand

Add \bind metacommand to set values for bind variables in SQL statements. A semicolon (;) is not considered a line terminator, so specify something like \g.

Example 96 Binding variable settings
postgres=> SELECT $1, $2
postgres-> \bind 'value1' 'value2' \g
 ?column? | ?column?
----------+----------
 value1   | value2
(1 row)
postgres=> SELECT * FROM data1 WHERE c1=$1 \bind 100 \g
 c1  |  c2
-----+-------
 100 | data1
(1 row)

PDF page 84

\pset metacommand

Add the xheader_width option to \pset metacommand. This option can limit the width of the header when outputting in extended table format. The values that can be specified are as follows.

ValueDescriptionNote
fullWidest line length (default)
columnTruncate to Width of First Column
pageTruncate to Terminal Page Width
Numerical valueTruncate to Specified Number
Table 34 Possible values
Example 97 Specifying the xheader_width option
postgres=> \x
Expanded display is on.
postgres=> CREATE TABLE data1(c1 char(10), c2 char(20)) ;
CREATE TABLE
postgres=> INSERT INTO data1 VALUES ('1234567890', '12345678901234567890') ;
INSERT 0 1
postgres=> \pset xheader_width
Expanded header width is 'full'.
postgres=> SELECT * FROM data1 ;
-[ RECORD 1 ]------------
c1 | 1234567890
c2 | 12345678901234567890
postgres=> \pset xheader_width column
Expanded header width is 'column'.
postgres=> SELECT * FROM data1 ;
-[ RECORD 1 ]
c1 | 1234567890
c2 | 12345678901234567890

PDF page 85

\dpS metacommand

‘S’ can now be specified for \dp meta-command and \z meta-command. Along with this, the operation of the \dp or \z command has been changed from the previous version. Temporary objects were included in the list, and objects in the information_schema schema were excluded.

\watch metacommand

Add validation of entered values. Any non-numeric or negative value will result in an error. Specify zero (0) to rerun without an interval. Also, the number of reruns can now be specified. To limit the number of re-executions, specify the interval and the number of executions in the “i=interval c=number of times of execution” format.

Example 98 \watch metacommand with rerun count
postgres=> SELECT current_time ;
    current_time
--------------------
16:58:33.012005+09
(1 row)
postgres=> \watch i=1 c=2
Sat 16 Sep 2023 04:58:34 PM JST (every 1s)
    current_time
--------------------
16:58:34.850061+09
(1 row)
Sat 16 Sep 2023 04:58:35 PM JST (every 1s)
    current_time
--------------------
 16:58:35.850556+09
(1 row)

PDF page 86

\drg metacommand

Added \drg meta command to output role grant information. [d65ddac]

Example 99 \drg metacommand
postgres=# GRANT pg_checkpoint TO demo1 WITH INHERIT FALSE ;
GRANT ROLE
postgres=# GRANT pg_checkpoint TO demo2 WITH SET FALSE ;
GRANT ROLE
postgres=# \drg
              List of role grants
 Role name |   Member of   | Options | Grantor
-----------+---------------+---------+----------
 demo1     | pg_checkpoint | SET     | postgres
 demo2     | pg_checkpoint | INHERIT | postgres
(2 rows)

3.4.13 Vacuumdb

The following options have been added to the vacuumdb command. [7781f4e, 4211fbd, ae78cae]

–schema option

Specify the name of the schema to which the table to be VACUUMed belongs. The shorthand option is -n. This option can be specified multiple times. This option cannot be used with the –excludeschema option.

–exclude-schema option

Specify the name of the schema to which the table that does not execute VACUUM belongs. The shorthand option is -N. This option cannot be used at the same time as the –schema option.

PDF page 87

Example 100 vacuumdb command with specifying schema
$ vacuumdb -d postgres --schema=public
vacuumdb: vacuuming database "postgres"
$ vacuumdb -d postgres --exclude-schema=public
vacuumdb: vacuuming database "postgres"
$ vacuumdb -d postgres --schema=public --exclude-schema=public
vacuumdb: error: cannot vacuum all tables in schema(s) and exclude schema(s) at
the same time
–no-process-main option

Added the option –no-process-main for the VACUUM (PROCESS_MAIN FALSE) statement.

Example 101 With the --no-process-main option
$ vacuumdb --no-process-main --echo postgres
SELECT pg_catalog.set_config('search_path', '', false);
vacuumdb: vacuuming database "postgres"
RESET search_path;
SELECT c.relname, ns.nspname FROM pg_catalog.pg_class c
 JOIN pg_catalog.pg_namespace ns ON c.relnamespace OPERATOR(pg_catalog.=)
ns.oid
 LEFT JOIN pg_catalog.pg_class t ON c.reltoastrelid OPERATOR(pg_catalog.=) t.oid
 WHERE c.relkind OPERATOR(pg_catalog.=) ANY (array['r', 'm'])
 ORDER BY c.relpages DESC;
SELECT pg_catalog.set_config('search_path', '', false);
VACUUM (PROCESS_MAIN FALSE, SKIP_DATABASE_STATS) pg_catalog.pg_proc;
…
–buffer-usage-limit option

Add the –buffer-usage-limit option for VACUUM (BUFFER_USAGE_LIMIT) statement.

PDF page 88

Example 102 The vacuumdb command with --buffer-usage-limit option
$ vacuumdb --buffer-usage-limit=1MB --echo postgres
SELECT pg_catalog.set_config('search_path', '', false);
vacuumdb: vacuuming database "postgres"
…
SELECT pg_catalog.set_config('search_path', '', false);
VACUUM (SKIP_DATABASE_STATS, BUFFER_USAGE_LIMIT '1MB') public.t1;
…