在一些场景中,可能要将数据库设置为只读模式。例如:需要对数据库进行迁移,准备割接时,首先要将主库切换到只读(锁定),确保绝对不会有新的事务写入,导致数据不一致的情况。
但实际上,目前 PostgreSQL 没有严格意义上的只读模式。不过 PostgreSQL 提供了 2 种只读锁定的方法:
通过或可以
硬锁定(调整参数):直接将数据库切换到恢复模式(Recovery Mode),不允许写操作。软锁定(设置事务模式):设置 system config default_transaction_read_only = on,将后续登录的会话或者当前事务设置为只读模式,允许被破解。在只读模式下,PostgreSQL 不允许执行如下 SQL:
When a transaction is read-only, the following SQL commands are disallowed: INSERT, UPDATE, DELETE, and COPY FROM if the table they would write to is not a temporary table; all CREATE, ALTER, and DROP commands; COMMENT, GRANT, REVOKE, TRUNCATE; and EXPLAIN ANALYZE and EXECUTE if the command they would execute is among those listed. This is a high-level notion of read-only that does not prevent all writes to disk.
注:PostgreSQL 12: Recovery.conf 文件参数合并到了 postgresql.conf,recovery.conf 不再使用。
重启数据库。 pg_ctl restart -m fast 硬锁定是不可被破解的。 postgres=# select pg_is_in_recovery(); pg_is_in_recovery ------------------- t (1 row) postgres=# insert into t1 values (1); ERROR: cannot execute INSERT in a read-only transaction postgres=# begin transaction read write; ERROR: cannot set transaction read-write mode during recovery注:PostgreSQL 12: Recovery.conf 文件参数合并到了 postgresql.conf,recovery.conf 不再使用。
重启数据库。 pg_ctl restart -m fast注:软锁定是可以被破解的,无需重新配置,执行指令:
psql -U <username> -d postgres begin; set transaction read write; alter database exercises set default_transaction_read_only = off; commit; \q