skip to Main Content

I’m working on a Phoenix/Elixir project which is managed using nix-shell. I’m having issues regarding postgres setup.

I’m starting server using pg_ctl -D $PGDATA start and it outputs as:

pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2021-09-01 19:19:00.780 PKT [72367] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2021-09-01 19:19:00.784 PKT [72367] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2021-09-01 19:19:00.806 PKT [72369] LOG:  database system was interrupted; last known up at 2021-09-01 19:14:57 PKT
2021-09-01 19:19:00.848 PKT [72369] LOG:  database system was not properly shut down; automatic recovery in progress
2021-09-01 19:19:00.849 PKT [72369] LOG:  redo starts at 0/16D84A0
2021-09-01 19:19:00.849 PKT [72369] LOG:  invalid record length at 0/16D8580: wanted 24, got 0
2021-09-01 19:19:00.849 PKT [72369] LOG:  redo done at 0/16D8548
2021-09-01 19:19:00.862 PKT [72367] LOG:  database system is ready to accept connections
 done
server started

when I execute createuser postgres --createdb --echo in nix-shell envoirnment it produces following error even though the run directory exists.

createuser: could not connect to database template1: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/run/postgresql/.s.PGSQL.5432"?

No other postgres instance is running.
In pg_hba.conf auth method is set to trust.
unix_socket_directories is set to /tmp.

shell.nix looks something like

{ pkgs ? import <nixpkgs> {} }:

with pkgs;

let
  inherit (lib) optional optionals;

  elixir = beam.packages.erlangR22.elixir_1_10;
  postgresql = postgresql_11;
in

mkShell {
  buildInputs = [
    ps
    elixir
    coreutils
    which
    git
    postgresql
    redis
    doxygen
    mongodb-tools
    redis-dump
    (python37.withPackages(ps: with ps; [ credstash awscli ]))
    cmake
    nix-prefetch-git
    zlib
    jq
    teleport
  ]);

  # Fix GLIBC Locale
  LOCALE_ARCHIVE = lib.optionalString stdenv.isLinux
    "${pkgs.glibcLocales}/lib/locale/locale-archive";
  LANG = "en_US.UTF-8";

  # Put the PostgreSQL and Redis databases in the project diretory.
  PGDATA="./.db/postgres";
  RDDATA="./.db/redis";

  shellHook = ''
  ERL_INCLUDE_PATH="${erlangR22}/lib/erlang/usr/include";
}

I totally a noob to Nix and have no idea how to get it working.
Any help would be appreciated, thanks.

2

Answers


  1. You have to tell createuser to connect via the socket in /tmp:

    createuser -h /tmp -U postgres --createdb --echo newuser
    
    Login or Signup to reply.
  2. You’ve set a custom path in the postgres config, so you probably also need to specify that with pqsl / createuser. In other words createuser -h /tmp/ ....

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search