In PostgreSQL 9.4 and above you can use ALTER SYSTEM SET
to modify postgresql.auto.conf
to add parameters.
So you could:
SHOW shared_buffers;
and if it's too low:
ALTER SYSTEM
SET shared_buffers = '512MB';
You must then restart PostgreSQL. This, by design, cannot be done from within PostgreSQL because if there's a problem during restart then you can't connect to it anymore, so you could lock yourself out too easily. You can reload the config with SELECT pg_reload_conf()
, but shared_buffers
only takes effect at full restart, not just a config reload.
So your app will need to use an external OS-specific and install-method-specific way to restart PostgreSQL once you've made the config change. Or just tell the user to restart it for you.
To restart:
For any platform (Windows, Mac or Linux) with PostgreSQL custom installed and managed, use pg_ctl restart
.
On Windows, installed as a service: use net stop [servicename]
then net start [servicename]
On older Linux (pre-systemd) with PostgreSQL installed system-wide: Use the service
command, e.g. service restart [servicename]
. This must be run as root or via sudo.
On newer Linux (systemd) with PostgreSQL installed system-wide: Use systemctl restart [servicename].service
If you're using an older PostgreSQL without ALTER SYSTEM SET
you'll have to open, modify and write postgresql.conf
.
To avoid having to parse and modify the whole postgresql.conf
I find it useful to append the include_dir = 'conf.d'
option and then create a conf.d/myapp.conf
in the datadir with just the config overrides I want.
include_dir
is available in 9.3 and newer. For still-older versions you've just got to modify postgresql.conf
directly.
If your app is responsible for installing and setting up PostgreSQL in the first place you can just set the desired shared_buffers
in the config file after initdb
and before starting up the database.
If you are bundling PostgreSQL in your application this is the approach I strongly prefer. Don't use the installer, just bundle the binaries in your installer. Start PostgreSQL when your app starts by invoking the postgres
command directly, or by using pg_ctl
. Stop it when your program exits. Use a non-default port (i.e. don't use port 5432), do not install under a postgres
user, put the data directory into something like %PROGRAMDATA%\MyApp\postgres
and otherwise avoid conflicting with official PostgreSQL installs. This will save you a lot of grief.
See: