How to use Postgres with NixOS

Hi, I’m trying to use postgresql, and my original attempt was to do nix-env -i postgresql, and it installed, but I ran into issues when trying to use it. The logfile was said it was missing /run/postgresql or something. After searching, I landed on adding a services.postgresql set to /etc/nixos/configuration.nix

That section now looks like this:

services.postgresql = {
  enable = true;
  package = pkgs.postgresql_11;
  dataDir = "/data/postgresql";
}

After applying the changes with nixos-rebuild and rebooting, I get the following errors:

$ psql
psql: FATAL: database "habbah" does not exist
$ psql -U postgres
psql: FATAL: Peer authentication failed for user "postgres"

Any help is appreciated!

1 Like

Global NixOS 23.11 manual | Nix & NixOS

Per proj

Edit: looks like I only read the title and the first part of your post. try createdb efore using psql.

1 Like
sudo -u postgres psql
3 Likes

createdb didn’t seem to work/

$ createdb
createdb: database creation failed: ERROR:  permission denied to create

@danbst That worked!