03 Lesson 03: Basic Tools / Server Configuration
This page is extracted from the official PDF into native Markdown. Slide text, code and speaker notes are kept in reading order; the original PDF and HTML remain the layout references. HTML source: dba1_03_tools_configuration.html Archive: DBA1-handouts-16-en.zip PDF pages: 22
Page 1: Basic Tools / Server Configuration
Copyright
© Postgres Professional, 2017–2025 Authors: Egor Rogov, Pavel Luzanov, Ilya Bashtanov, Aleksei Beresnev Translated by: Liudmila Mantrova, Alexander Meleshko, Elena Sharafutdinova Photo: Oleg Bartunov (Phu Monastery and Bhrikuti Peak, Nepal)
Use of Course Materials
Non-commercial use of course materials (presentations, demonstrations) is allowed without restrictions. Commercial use is possible only with the written permission of Postgres Professional. It is prohibited to make changes to the course materials.
Feedback
Please send your feedback, comments and suggestions to: edu@postgrespro.ru
Disclaimer
Postgres Professional assumes no responsibility for any damages and losses, including loss of income, caused by direct or indirect, intentional or accidental use of course materials. Postgres Professional company specifically disclaims any warranties on course materials. Course materials are provided “as is,” and Postgres Professional company has no obligations to provide maintenance, support, updates, enhancements, or modifications.
Page 2: Topics
- Configuration Parameters
- Configuration Files
- Parameter Management at the Instance and Session Levels
Page 3: Configuration Parameters
Purpose
- managing DBMS operation and behavior
- Setting parameters
- for an instance via configuration files
- for a separate database or user
- for the current session
Speaker notes
There are multiple parameters in PostgreSQL that control the DBMS behavior. These parameters affect resource management, server processes, and much more.
For example, the max_connections parameter limits the number of concurrent connections to the server.
The full list of configuration parameters and their descriptions is available in the documentation: https://postgrespro.com/docs/postgresql/16/runtime-config
In this topic, we will not cover any specific configuration parameters, but rather discuss how to set their values.
Configuration parameters are generally managed in configuration files. The parameter values set in the configuration files affect the whole DBMS instance, unless explicitly configured otherwise.
Some parameters can be set for a specific database or for a specific user’s sessions. Such settings will overrule those declared in configuration files. These types of settings will be discussed in further lessons of the course.
Lastly, many parameters can be changed at the session level, during client operaton.
Page 4: postgresql.conf
Main configuration file
- loaded when the server starts
- option to load additional configuration files
- located in the data directory (PGDATA) by default
- After any changes to the parameters, the file has to be reloaded
- => SELECT pg_reload_conf();
- changes to some parameters require a server restart to apply
Speaker notes
The main configuration file is postgresql.conf.
The default file’s location is defined during initial PostgreSQL compilation. The server executable accepts -c config_file as a command line argument to set the configuration file path.
By default, the file is located in the data catalog (PGDATA), but package distributions usually place it somewhere else, depending on the OS-specific conventions.
This is a well-documented plaintext file which stores parameters in a key-value format.
Additional configuration files can be included. By default, PostgreSQL on Ubuntu automatically includes all .conf files from /etc/postgresql/16/main/conf.d.
If the same parameter is defined in the configuration files multiple times, only the most recently read value will be used.
For any changes to parameters to apply, the file must be reloaded. Some parameters require a server restart to apply.
Page 5: The postgresql.conf File and the pg_file_settings View
The configuration file name is a value of the read-only config_file parameter. The configuration file can be specified using a command-line switch when starting postgres.
Take a look at a part of a config file.
The main configuration file postgresql.conf can include additional configuration files. Directives: include_dir is a directory with additional configuration files. include includes an additional configuration file. include_if_exists Includes an additional configuration file, if one exists. These directives are typically placed at the end of postgresql.conf:
To see current settings in configuration files, query the pg_file_settings view:
Page 6: Page 6
The view displays non-comment lines of configuration files. The applied column indicates whether the specified value will be applied upon reloading the configuration. Specifically, the column will show false if: The change requires a restart of the server. There is a line with the same parameter that will be read later. There is an error in one of the lines where the parameter is specified. The view also displays the configuration file name and line number, making it easy to locate errors.
The pg\_settings View
For example, let’s take the work_mem parameter. It defines how much memory is allocated for operations like sorting or hash join. The default value can be insufficient for some queries. To learn more about the work_mem parameter, check out our Query Performance Tuning course (QPT). Active configuration parameter values are shown in the pg_settings view. For example, here’s its output regarding the work_mem parameter:
Page 7: Page 7
{#source-figure-007 caption=“Source slide page 7: Page 7”}
Below are the main columns of the pg_settings view: name, unit — parameter name and unit setting — current value boot_val — default value reset_val — initial value for sessions source — source of the current parameter value sourcefile, sourceline — configuration file and line number, if the current value was specified in the file pending_restart — true if the value is changed in the configuration file, but server restart is required to be applied The context column shows what action has to be taken for the parameter change to take effect. Possible values include: internal — cannot be changed, the value is set during installation postmaster — server restart required sighup — requires a reload of the configuration files superuser — superuser can change for their session user — any user can change for their session
Order of Applying Settings
When reloading the configuration, the main file is read first, followed by additional files. If the same parameter is defined multiple times, the latest value is applied. For example, let’s specify the work_mem parameter twice in an additional configuration file:
Contents of the /etc/postgresql/16/main/conf.d/work_mem.conf file:
The applied = f for the first line indicates that it will not be applied. The context field of the work_mem parameter says “user”. This means that the parameter can be changed by the user during their session. We will discuss how to do it in a bit. To change the value for all sessions, you just need to reload the configuration file:
Verify that work_mem now has taken the value from the second line of the configuration file:
Page 8: Page 8
Page 9: postgresql.auto.conf
Configuration file managed by SQL commands
- ALTER SYSTEM
- adds or changes a line
- SET parameter TO value;
- ALTER SYSTEM RESET parameter;
- deletes a line
- ALTER SYSTEM RESET ALL;
- deletes all lines
- Location
- always in the data directory (PGDATA)
- Actions needed to apply
Speaker notes
The file postgresql.auto.conf is always loaded last. This file is always located in the data catalog (PGDATA).
It should never be modified manually, but only with the ALTER SYSTEM command. ALTER SYSTEM is an SQL interface for managing configuration parameters.
For any changes made with ALTER SYSTEM to take place, the server must reload the configuration files, as it does with postgresql.conf.
The contents of both files (postgresql.conf and postgresql.auto.conf) can be checked using the pg_file_settings view, and the current parameter values are shown in the pg_settings view.
More about the ALTER SYSTEM command:
https://postgrespro.com/docs/postgresql/16/sql-altersystem
Page 10: The ALTER SYSTEM Command and the postgresql.auto.conf File
Let’s set a new value for the work_mem parameter:
What happened? ALTER SYSTEM checks if the provided value is valid.
Now it works! The command saves the new value 16MB to a file named postgresql.auto.conf:
However, the value has not been applied yet:
For the new work_mem setting to apply, reload the configuration file first:
Lines are removed from postgresql.auto.conf with the ALTER SYSTEM RESET command:
Page 11: Page 11
Reload the configuration file again. The old value from work_mem.conf is now applied:
Page 12: Current Session
{#source-figure-012 caption=“Source slide page 12: Current Session”}
set until the end of the session or transaction
- setting parameters is transactional
- custom parameters are allowed
Speaker notes
Parameter values can be changed directly during the session with the SET command or the set_config function. The SHOW command or the current_setting function display the current parameter value.
By setting a new value, you can specify its expiration date: until the end of the session (by default) or until the end of the transaction (SET LOCAL).
In any case, setting parameters is transactional: if the current transaction is rolled back, any parameters modified within it will return to the state they were in at the start of the transaction.
In addition to the PostgreSQL system parameters, the same commands and functions can be used to create and get the values of custom parameters.
Page 13: Setting Parameters for the Current Session
The SET command is used to set parameter values for the duration of the current session:
The set_config function also works:
The third parameter of the function defines if the parameter should be used for the duration of the current transaction (true) or until the end of the current session (false). This may be important when using a connection pool, when transactions by multiple users may be performed within the same session.
Checking Parameter Values During a Session
There are multiple ways to check the current parameter value:
Reset the value to what was active at the start of the session:
Setting Parameter Values within a Transaction
Open a transaction and set a new work_mem value:
Page 14: Page 14
If the transaction is rolled back, so is the new parameter value. If the transaction commits, however, the parameter remains changed.
You can set a new parameter value just for the duration of the current transaction:
When the transaction commits, the old value returns:
Custom Parameters
Custom parameters can be defined during session. You can also check if such a parameter is already defined. Custom parameter names must include the dot (.) in order to distinguish them from standard parameters.
Now myapp.currency_code can be used as a global variable during the session:
Page 15: Page 15
Custom parameters can be defined in configuration files. This will make them initialize in all sessions.
Page 16: Takeaways
The main configuration file is postgresql.conf
- ALTER SYSTEM is an SQL interface for managing configuration parameters stored in postgresql.auto.conf
- When configuration files are modified, they have to be reloaded
- Some parameters can be changed just for the current session
- Changes to some parameters require a server restart to apply
Page 17: Practice
- Get a list of parameters that require a server restart to apply.
- In the additional included configuration file, make an error when changing the max_connections parameter.
- Restart the server. Make sure that the server does not start and
- check the message log.
- Fix the error and start the server.
Speaker notes
- To get the location of the postgresql.conf file, check the value of the config_file parameter, as shown in the demonstration.
At the end of this file, you will find include_dir directive that loads additional configuration files.
Create a file named max_connections.conf in the directory specified by this directive. Edit this file either as the owner (the postgres user) or as a superuser.
To obtain the necessary privileges, use the sudo command.
Use your preferred text editor. For example: sudo nano /etc/postgresql/16/main/conf.d/max_connections.conf
To exit nano, use the Ctrl+X key combination.
Page 18: 1. Parameters that Require a Server Restart to Apply
2. Setting the max\_connections Parameter
The current value of max_connections:
Page 19: Page 19
Imagine we try to set it to 50, but mistype and enter the letter O instead of the zero:
The error can be discovered by looking at the pg_file_settings view:
But here, we didn’t look at pg_file_settings and restarted the server right away:
The server does not start. The reason is recorded in the server log. Here is what it says:
Let’s fix the error:
The server is not running, so we’ll use an operating system command to verify.
Restart the server:
The server starts. Check the max_connections value:
Page 20: Page 20
Page 21: Practice+
- Set the parameter work_mem = 32MB in the psql tool’s command line options. 2. In the Ubuntu package distribution, the postgresql.conf file is not located in the PGDATA directory. How does the server find this configuration file at startup?
Speaker notes
- Use either the options key in the connection string or the PGOPTIONS environment variable.
More on how connection strings are formed:
https://postgrespro.com/docs/postgresql/16/libpq-connect#LIBPQ-CONNST RING
- To see the location of the postgresql.conf file, check the config_file parameter.
To find where the parameter is set, run the ps command for the postgres main process. The process ID (PID) is the first line of the postmaster.pid file, which is located in the data directory (PGDATA).
Page 22: 1. Setting Parameters at Application Start
If the app uses the libpq library to connect to the server, there are two ways you can set parameters at application start. The first way is to add the options key to the connection string:
The second way is set the PGOPTIONS environment variable:
2. The config\_file Variable
The file postgresql.conf isn’t located within the data catalog:
How does the server know where postgresql.conf is? In the Ubuntu package distribution, the config_file value is defined in the server startup command text. This allows the postgresql.conf file to be stored outside of PGDATA. To check the server’s startup command line, you can use the ps command. Find the PID of the main server process – it’s listed in the first line of the postmaster.pid file located in the PGDATA directory:
Now we can retrieve the postmaster process command line: