Sub topic 5
Where configuration and data live
A database server reads settings from text files and keeps its data in one directory tree. Knowing both locations lets you change behavior safely, find the right files for backups, and read the server's state when something breaks. This chapter covers where PostgreSQL and MySQL keep these files on Linux, how to ask a running server for the paths, and how settings are layered.
It assumes you have installed a server from distribution packages (see the earlier installation chapter), can use sudo, and know how systemctl starts and stops a service.
Two kinds of files
Every server separates two things:
- Configuration is small, hand-edited text. It says how the server behaves: which addresses to listen on, how much memory to use, who may connect.
- The data directory is the server's own storage. It holds table files, transaction logs, and internal state. The server manages these files in a binary format. Never edit them by hand.
Packages put the two in different places on purpose. You can back up, version-control, and review configuration without touching terabytes of data.
PostgreSQL layout
Paths depend on the distribution family.
| Item | Debian and Ubuntu | RHEL, Rocky, Fedora (PGDG packages) |
|---|---|---|
| Main settings | `/etc/postgresql/16/main/postgresql.conf` | `/var/lib/pgsql/16/data/postgresql.conf` |
| Host-based authentication | `/etc/postgresql/16/main/pg_hba.conf` | `/var/lib/pgsql/16/data/pg_hba.conf` |
| Data directory | `/var/lib/postgresql/16/main` | `/var/lib/pgsql/16/data` |
The 16 is the major version and main is the cluster name. A PostgreSQL cluster is one server instance with one data directory, and it can hold many databases. Debian's wrapper tools let several clusters coexist, and pg_lsclusters lists them with their ports and data directories.
On RHEL-family systems, configuration lives inside the data directory. A backup of that directory therefore includes the settings, but the files are still separate in purpose.
Asking the server instead of guessing
Do not trust remembered paths. A running server can report where it read its files:
SHOW config_file;
SHOW hba_file;
SHOW data_directory;If you cannot connect, ps shows the -D or -c config_file= arguments the service was started with.
What is inside the data directory
| Entry | Purpose |
|---|---|
| `PG_VERSION` | Major version that created the cluster |
| `base/` | One subdirectory per database, holding table and index files |
| `global/` | Cluster-wide catalogs, such as roles |
| `pg_wal/` | Write-ahead log (WAL), the record of changes used for crash recovery and replication |
| `postmaster.pid` | Process ID and lock of the running server |
| `postgresql.auto.conf` | Settings written by `ALTER SYSTEM` |
The directory must be owned by the postgres user with mode 0700. PostgreSQL refuses to start if group or other users can access it.
MySQL layout
MySQL's data directory is almost always /var/lib/mysql, owned by the mysql user. Configuration is assembled from several files that include one another.
| Item | Debian and Ubuntu | RHEL-family |
|---|---|---|
| Entry point | `/etc/mysql/my.cnf` | `/etc/my.cnf` |
| Included fragments | `/etc/mysql/conf.d/` and `/etc/mysql/mysql.conf.d/` | `/etc/my.cnf.d/` |
Ask the server for its paths and for the order in which it searches files:
mysqld --verbose --help 2>/dev/null | grep -A1 'Default options'
mysql -e 'SELECT @@datadir, @@basedir, @@socket'Inside the data directory you will find one subdirectory per database, .ibd files that hold InnoDB tables, the redo log (#innodb_redo/ in MySQL 8.0.30 and later), binary logs if enabled, and mysqld-auto.cnf, which stores settings changed with SET PERSIST.
How settings are layered
Both servers combine several sources. A later source overrides an earlier one, so a value you set may be silently replaced.
For PostgreSQL, the order of precedence is:
- Built-in defaults.
postgresql.conf, including any files pulled in byinclude_dir(Debian shipsconf.d/this way).postgresql.auto.conf, written byALTER SYSTEM. It is read last among the files.- Command-line options from the service unit.
- Per-database or per-role overrides set with
ALTER DATABASE ... SETorALTER ROLE ... SET.
For MySQL, option files are read in the order the search reports. If an option appears more than once, the last occurrence wins. mysqld-auto.cnf is applied after the regular files.
The practical rule is to put your changes in a small drop-in file instead of editing the packaged file. Package upgrades may replace the original, and a drop-in shows exactly what you changed:
# /etc/postgresql/16/main/conf.d/10-local.conf
shared_buffers = 1GB
log_min_duration_statement = 500Confirm which value is active and where it came from:
SELECT name, setting, context, sourcefile, sourceline
FROM pg_settings
WHERE name IN ('shared_buffers', 'listen_addresses');MySQL offers the same check through performance_schema.variables_info:
SELECT v.VARIABLE_NAME, v.VARIABLE_VALUE, i.VARIABLE_SOURCE, i.VARIABLE_PATH
FROM performance_schema.global_variables v
JOIN performance_schema.variables_info i USING (VARIABLE_NAME)
WHERE v.VARIABLE_NAME = 'innodb_buffer_pool_size';Applying a change
Not every setting takes effect the same way.
- PostgreSQL labels each setting with a
contextinpg_settings.sighupsettings apply aftersudo systemctl reload postgresql.postmastersettings, such aslisten_addressesandshared_buffers, needsudo systemctl restart postgresql. Ifpending_restartis true inpg_settings, the file has changed but the running server has not picked it up. - MySQL has no general reload for option files. Dynamic variables can change live with
SET GLOBAL(lost at restart) orSET PERSIST(kept inmysqld-auto.cnf). Static variables need a restart.
Check syntax before restarting. A typo in a configuration file can stop the service from starting. PostgreSQL's pg_settings view has a pending_restart column, and mysqld --validate-config checks MySQL's files.
Moving or resizing the data directory
You may need to move data to a larger volume. The steps are the same in outline for both engines:
- Stop the service.
- Copy the directory with ownership and permissions preserved, for example
sudo rsync -a /var/lib/mysql/ /srv/mysql/. - Point the configuration at the new path (
data_directoryordatadir). - Fix mandatory access control. On Ubuntu, AppArmor blocks MySQL from the new path until you add an alias rule for it. On RHEL-family systems, SELinux needs the correct file context, for example with
semanage fcontextandrestorecon. - Start the service and check the logs.
If the server fails with "permission denied" after a move and file ownership looks right, suspect AppArmor or SELinux. The kernel log or journalctl shows the denial.
Common mistakes
- Editing
postgresql.confand expecting anALTER SYSTEMvalue to lose. The auto file wins. - Running two servers against one data directory. The pid file normally prevents this, but a stale file after a crash can mislead you. Check the process list before deleting it.
- Copying a live data directory as a backup. The copy is usually inconsistent. Use the tools from the backup chapter.
- Putting the data directory on a full volume. Both engines stop accepting writes when WAL or redo space runs out.
Exercise
You added max_connections = 200 to postgresql.conf and reloaded the service, but SHOW max_connections; still reports 100. Name two likely causes and how to check each.
Show answer
First, max_connections has context postmaster, so it needs a restart, not a reload. Check SELECT name, setting, pending_restart FROM pg_settings WHERE name = 'max_connections'; and look for pending_restart = t. Second, another source may override the value, such as postgresql.auto.conf or a later drop-in file. Check the sourcefile and sourceline columns in pg_settings to see which file supplied the active value.