Reputation: 11
I want to write a script to configure postgresql.conf file, in which for every parameter it'll show the existing value and after that asked for updated value, then user will enter the updated value.
I'm unable to update the postgresql.conf files parameter through the update command while i can do the alter.
suppose i want to update "shared_preload_libraries" parameter in postgresql.conf which has already some values like
shared_preload_libraries='$libdir/dbms_pipe,$libdir/edb_gen,$libdir/dbms_aq '
Now i want to add 'pgaudit' like shared_preload_libraries='pgaudit,$libdir/dbms_pipe,$libdir/edb_gen,$libdir/dbms_aq '
i want to do the same through the command
I tried alter command only but it's not the way to update
Upvotes: 1
Views: 2633
Reputation: 753
postgresqlconf.sql
.docker cp ./postgresql.conf
my-postgres:/var/lib/postgresql/data/postgresql.auto.conf
docker restart my-postgres
NOTE: postgresql.auto.conf
is what postgres updates it's postgresql.conf
from on restart.
Upvotes: 0
Reputation: 25738
In addition to
postgresql.conf
, a PostgreSQL data directory contains a filepostgresql.auto.conf
, which has the same format aspostgresql.conf
but is intended to be edited automatically, not manually. This file holds settings provided through theALTER SYSTEM
command. This file is read wheneverpostgresql.conf
is, and its settings take effect in the same way. Settings inpostgresql.auto.conf
override those inpostgresql.conf
.
You can interact with the config via SQL commands SHOW
and SET
or functions current_setting()
and set_config()
:
select set_config('shared_preload_libraries',
'pgaudit,'||current_setting('shared_preload_libraries'),
false);
Unfortunately, in case of shared_preload_libraries
this will fail with ERROR: parameter "shared_preload_libraries" cannot be changed without restarting the server
. This type of setting can only be changed directly or through ALTER SYSTEM SET...
, in case of which the new value will be just saved to postgresql.auto.conf
until you restart the server because it only applies at startup. If you want to load a library after startup, use LOAD
.
ALTER SYSTEM
can't be issued dynamically from functions and prepared statements or given a function:
ALTER SYSTEM
SET shared_preload_libraries = 'pgaudit'
|| current_setting('shared_preload_libraries');
--ERROR: syntax error at or near "||"
--LINE 3: || current_setting('shared_pr...
-- ^
prepare statement1 (text) as
alter system set shared_preload_libraries to $1;
execute statement1('pgaudit,'
||current_setting('shared_preload_libraries'));
--ERROR: syntax error at or near "alter"
--LINE 1: prepare statement1 (text) as alter system set shared_preload...
-- ^
do $$
declare current_setting text;
begin
select current_setting('shared_preload_libraries') into current_setting;
current_setting=rtrim(concat_ws(',','pgaudit','test',current_setting),',');
execute format ('alter system set shared_preload_libraries to %s',current_setting);
end $$;
--ERROR: ALTER SYSTEM cannot be executed from a function
--CONTEXT: SQL statement "alter system set shared_preload_libraries to pgaudit,test"
--PL/pgSQL function inline_code_block line 8 at EXECUTE
Upvotes: 1