Christophe Pettus: Tutti i tuoi GUC in fila: data_directory
Sintesi redazionale: PostgreSQL risolve un dilemma di avvio grazie a `data_directory`: come individuare il file di configurazione prima di sapere dove risiedono i dati.. Fonte originale: https://postgr.es/p/9lq
<p>`data_directory`</p><p>names the location of the cluster’s data — the directory people mean when they write `$PGDATA`</p><p>, the one holding `base/`</p><p>, `pg_wal/`</p><p>, `global/`</p><p>, and the rest. Context is `postmaster`</p><p>: settable in `postgresql.conf`</p><p>or on the command line, never at runtime. And like a small handful of others, it cannot be set with `ALTER SYSTEM`</p><p>— for the obvious reason that `ALTER SYSTEM`</p><p>writes to `postgresql.auto.conf`</p><p>, which lives in the data directory, which is the thing you’d be trying to locate. You can’t use a file inside the box to tell PostgreSQL where the box is.</p><p>That circularity is the whole story of this parameter, so let’s pull on it.</p><p>## The bootstrapping puzzle</p><p>If `data_directory`</p><p>can be set in `postgresql.conf`</p><p>, and `postgresql.conf`</p><p>normally lives *in* the data directory, how does PostgreSQL find the config file in order to read where the data directory is? The answer is that in the ordinary case it doesn’t need to. `postgres`</p><p>is started with `-D`</p><p>(or the `PGDATA`</p><p>environment variable) pointing at the data directory, the config files are found sitting inside it, and `data_directory`</p><p>is never set at all — the `-D`</p><p>value is the data directory, full stop. The parameter exists for the *other* case.</p><p>That other case is keeping configuration somewhere other than the data. Then the roles split: `-D`</p><p>(or `PGDATA`</p><p>) points at the directory holding the config files, PostgreSQL reads `postgresql.conf`</p><p>from there, and `data_directory`</p><p>inside that file tells it where the actual data lives. The bootstrap resolves because `-D`</p><p>always identifies *where the config is*, and `data_directory`</p><p>, once read, overrides `-D`</p><p>for *where the data is* — but not for where the config is. Those are two separate questions and PostgreSQL answers them from two separate places.</p><p>This is exactly how Debian and Ubuntu lay things out. Their packaging puts `postgresql.conf`</p><p>under `/etc/postgresql/<version>/<cluster>/`</p><p>and the data under `/var/lib/postgresql/<version>/<cluster>/`</p><p>, with `data_directory`</p><p>in the config file wiring the two together. Config belongs to the sysadmin’s world under `/etc`</p><p>; data belongs under `/var/lib`</p><p>. Red Hat’s packaging, by contrast, keeps the traditional all-in-one layout with config inside the data directory. Neither is wrong; they’re different conventions, and `data_directory`</p><p>is the hinge that makes the split-out one possible.</p><p>The companion parameters `config_file`</p><p>, `hba_file`</p><p>, and `ident_file`</p><p>finish the job. `config_file`</p><p>can *only* be given on the command line — same circularity, it’s the file that would have to contain its own location — while `hba_file`</p><p>and `ident_file`</p><p>can live inside `postgresql.conf`</p><p>. Set all of those plus `data_directory`</p><p>explicitly and you needn’t pass `-D`</p><p>or `PGDATA`</p><p>at all, though almost nobody assembles a cluster that way by hand.</p><p>## What to do with it</p><p>For the overwhelming majority of installations: nothing. If you installed from a distribution package, the packaging already set `data_directory`</p><p>(or didn’t, on Red Hat-style layouts) to match its filesystem conventions, and the init script or systemd unit passes the right `-D`</p><p>. Changing it means moving the data directory, which is a stop-the-server, move-the-files, fix-the-unit-file operation — and the parameter is the last and easiest part of it, not the hard part.</p><p>If you’re building a cluster by hand and want config under `/etc`</p><p>in the Debian style, this is the parameter that does it: point `-D`</p><p>at the config directory, set `data_directory`</p><p>to the data location, done. Otherwise, leave it to the packaging, and when you need to know where your data actually is, `SHOW data_directory`</p><p>will tell you without your having to reason about which of `-D`</p><p>, `PGDATA`</p><p>, and the config file won.</p>
