This module provides a declarative interface for configuring PostgreSQL on NixOS. It supports initial database setup, dump restoration, authentication modes, logging configuration and TCP settings.
- Provide a unified PostgreSQL configuration for all hosts
- Support both fresh installations and database restoration
- Avoid manual SQL initialization steps
- Integrate cleanly with migration scripts
- Ensure safe and predictable PostgreSQL behavior
This module activates only when postgresql.enable = true.
- Version selection
- Initial setup (role, password, database)
- Dump restoration
- Authentication modes
- TCP enable/disable
- Logging configuration
Enables the PostgreSQL service.
PostgreSQL version to install. Defaults to pkgs.postgresql_17.
Port for the PostgreSQL server. Defaults to 5432.
The module supports automatic database initialization:
postgresql.initialSetup = {
enable = true;
role = "myuser";
passwordFile = "/path/to/password.txt";
database = "mydb";
};When enabled, the module:
- Creates the role if it doesn't exist
- Sets the password using the provided file
- Creates the database
- Grants privileges
If initialSetup.enable = true, then:
rolemustn't be emptypasswordFilemustn't be nulldatabasemustn't be empty
These are enforced via Nix assertions.
Instead of initial setup, you may restore a SQL dump:
postgresql.dumpFile = "/path/to/dump.sql";This generates an initialScript that runs:
\i /path/to/dump.sql
You cannot use both:
initialSetup.enable = truedumpFile != null
at the same time. This is enforced by assertions.
The module configures pg_hba.conf using:
postgresql.authMode = "trust" | "md5" | "scram";This affects:
- local connections
- IPv4 localhost
- IPv6 localhost
Example:
postgresql.authMode = "scram";postgresql.tcp.enable = true;Controls:
services.postgresql.enableTCPIPpostgresql.logStatements = "all" | "mod" | "none";Controls:
settings.log_statement| Feature | initialSetup | dumpFile | Notes |
|---|---|---|---|
| Role creation | ✔️ | ❌ | Only in initial setup |
| Password assignment | ✔️ | ❌ | Uses passwordFile |
| Database creation | ✔️ | ❌ | Only in initial setup |
| Dump restoration | ❌ | ✔️ | Uses \i dump.sql |
| Authentication | ✔️ | ✔️ | Via authMode |
| Logging | ✔️ | ✔️ | Via logStatements |
{pkgs, ...}: {
# ... other host configurations ...
postgresql = {
enable = true;
initialSetup = {
enable = true;
role = "app";
passwordFile = "/home/app/pgpass.txt";
database = "appdb";
};
authMode = "scram";
};
# ... other host configurations ...
}{pkgs, ...}: {
# ... other host configurations ...
postgresql = {
enable = true;
dumpFile = "/home/app/backup.sql";
authMode = "md5";
};
# ... other host configurations ...
}{pkgs, ...}: {
# ... other host configurations ...
postgresql = {
enable = true;
authMode = "trust";
logStatements = "all";
};
# ... other host configurations ...
}The migration scripts:
deploy-migration.shnixos-rebuild-migration.sh
depend on this module because they:
- detect PostgreSQL version changes
- create backups
- restore dumps
This module ensures that PostgreSQL is configured consistently across hosts.
Use it when:
- A host needs PostgreSQL
- You want declarative initialization
- You want safe upgrades and migrations
Do not use it for:
- External PostgreSQL servers
- Containers with ephemeral storage