PostgreSQL

PostgreSQL is a powerful, open source object-relational database system with over 35 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance.

Getting Started

# In `perSystem.process-compose.<name>`
{
  services.postgres."pg1".enable = true;
}

Examples

Guide

Creating users & tables

Assuming your initial schema is defined in ./scripts/db.sql:

# In `perSystem.process-compose.<name>`
{
  services.postgres."pg1" = {
    enable = true;
    initialScript.before = ''
      CREATE USER myuser WITH password 'mypasswd';
    '';
    initialDatabases = [
      {
        name = "mydb";
        schemas = [ ./scripts/db.sql ];
      }
    ];
  };
}

Gotchas

Unix-domain socket path is too long

Warning

Only relevant if socketDir is set. If not, postgres uses TCP/IP by default.

We already talk about this in the data directory guide. In case of postgres, you can set socketDir while keeping the dataDir unchanged.

Note

The socketDir must be set to a shorter path (less than 100 chars) as a workaround.

{
  services.postgres."pg1" = {
    enable = true;
    socketDir = "/tmp/pg1";
  };
}
Links to this page