pg_readonly

cluster database read only

Overview

PackageVersionCategoryLicenseLanguage
pg_readonly1.0.4ADMINPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
5120pg_readonlyNoYesNoYesNoNo-
Relatedpg_permissions pg_upless safeupdate set_user pgaudit noset sepgsql login_hook

Version

TypeRepoVersionPG VerPackageDeps
EXTMIXED1.0.41817161514pg_readonly-
RPMPGDG1.0.41817161514pg_readonly_$v-
DEBPIGSTY1.0.41817161514postgresql-$v-pg-readonly-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
d13.x86_64
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
d13.aarch64
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
u22.x86_64
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
u22.aarch64
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
u24.x86_64
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
u24.aarch64
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4
PIGSTY 1.0.4

Build

You can build the DEB packages for pg_readonly using pig build:

pig build pkg pg_readonly         # build DEB packages

Install

You can install pg_readonly directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pg_readonly;          # Install for current active PG version
pig ext install -y pg_readonly -v 18  # PG 18
pig ext install -y pg_readonly -v 17  # PG 17
pig ext install -y pg_readonly -v 16  # PG 16
pig ext install -y pg_readonly -v 15  # PG 15
pig ext install -y pg_readonly -v 14  # PG 14
dnf install -y pg_readonly_18       # PG 18
dnf install -y pg_readonly_17       # PG 17
dnf install -y pg_readonly_16       # PG 16
dnf install -y pg_readonly_15       # PG 15
dnf install -y pg_readonly_14       # PG 14
apt install -y postgresql-18-pg-readonly   # PG 18
apt install -y postgresql-17-pg-readonly   # PG 17
apt install -y postgresql-16-pg-readonly   # PG 16
apt install -y postgresql-15-pg-readonly   # PG 15
apt install -y postgresql-14-pg-readonly   # PG 14

Create Extension:

CREATE EXTENSION pg_readonly;

Usage

pg_readonly: cluster database read only

pg_readonly sets all databases in a PostgreSQL cluster to read-only mode at the SQL level. It must be loaded via shared_preload_libraries. The read-only status is managed in shared memory with a global flag (not persisted across restarts).

Check Read-Only Status

SELECT get_cluster_readonly();
-- Returns false (read-write) or true (read-only)

Set Cluster Read-Only

SELECT set_cluster_readonly();

In read-only mode, SELECT statements are allowed (unless they call writing functions), but DML (INSERT, UPDATE, DELETE), DDL (including TRUNCATE), and DCL (GRANT, REVOKE) are blocked:

SELECT * FROM t;              -- OK
UPDATE t SET x = 33;          -- ERROR: pg_readonly: invalid statement because cluster is read-only
CREATE TABLE tmp(c text);     -- ERROR: pg_readonly: invalid statement because cluster is read-only

Note: set_cluster_readonly() terminates all open transactions.

Set Cluster Read-Write

SELECT unset_cluster_readonly();

Note: background processes (checkpointer, bgwriter, walwriter, autovacuum) continue running in read-only mode – the restriction is at the SQL statement level only.


Last Modified 2026-03-12: add pg extension catalog (95749bf)