Search code examples
postgresqlconfigurationnixos

NixOS error: psql: FATAL: role "postgres" does not exist


I am attempting to use postgresql on NixOS, and face the following error when running $ psql -U postgres

$ psql -U postgres
psql: FATAL:  role "postgres" does not exist

I get a similar error when simply running $ psql, using the default user (my username). It appears that my postgres installation does not have a role that I can use to create other roles or run any commands.

How can I create a role for my postgres user so that I can issue commands?

I have installed postgres with $ nix-env -i postgres and configured in accordance with the NixOS manual, adding

services.postgresql.enable = true;
services.postgresql.package = pkgs.postgresql94;

to my /etc/nixos/configuration.nix configuration file.

I have also added postgres authentication as suggested in this example configuration, so the postgresql lines of my /etc/nixos/configuration.nix file look like

  # postgres
  services.postgresql.enable = true;
  services.postgresql.package = pkgs.postgresql94;
  services.postgresql.authentication = lib.mkForce ''
    # Generated file; do not edit!
    # TYPE  DATABASE        USER            ADDRESS                 METHOD
    local   all             all                                     trust
    host    all             all             127.0.0.1/32            trust
    host    all             all             ::1/128                 trust
    '';

Solution

  • In NixOS when the database cluster is initialized (using postgres' initdb) the database super user is set to root rather that the default postgres. So psql -U root should do the trick.