postgresql_16:4.5.1.3 Parameter Interaction via SQL
PostgreSQL 16 > Server Administration > Server Configuration > Setting Parameters > Parameter Interaction via SQL
PostgreSQL provides three SQL commands to establish configuration defaults. The already-mentioned ALTER SYSTEM command provides an SQL-accessible means of changing global defaults; it is functionally equivalent to editing postgresql.conf. In addition, there are two commands that allow setting of defaults on a per-database or per-role basis:
- The
ALTER DATABASE(see postgresql_16:7.1.5) command allows global settings to be overridden on a per-database basis. - The
ALTER ROLE(see postgresql_16:7.1.24) command allows both global and per-database settings to be overridden with user-specific values.
Values set with ALTER DATABASE and ALTER ROLE are applied only when starting a fresh database session. They override values obtained from the configuration files or server command line, and constitute defaults for the rest of the session. Note that some settings cannot be changed after server start, and so cannot be set with these commands (or the ones listed below).
Once a client is connected to the database, PostgreSQL provides two additional SQL commands (and equivalent functions) to interact with session-local configuration settings:
- The
SHOW(see postgresql_16:7.1.177) command allows inspection of the current value of any parameter. The corresponding SQL function iscurrent_setting(setting_name text)(see (see postgresql_16:3.6.27.1)). - The
SET(see postgresql_16:7.1.172) command allows modification of the current value of those parameters that can be set locally to a session; it has no effect on other sessions. Many parameters can be set this way by any user, but some can only be set by superusers and users who have been grantedSETprivilege on that parameter. The corresponding SQL function isset_config(setting_name, new_value, is_local)(see (see postgresql_16:3.6.27.1)).
In addition, the system view pg_settings (see postgresql_16:8.3.24) can be used to view and change session-local values:
- Querying this view is similar to using
SHOW ALLbut provides more detail. It is also more flexible, since it's possible to specify filter conditions or join against other relations. - Using
UPDATEon this view, specifically updating thesettingcolumn, is the equivalent of issuingSETcommands. For example, the equivalent of SET configuration_parameter TO DEFAULT; is: UPDATE pg_settings SET setting = reset_val WHERE name = 'configuration_parameter';