Createdb: could not connect to database template1:

Though this might not directly related to nix. I follow the doc[1] here trying to create an isolated environment with postgresql installed. However, when I execute the command createdb mydb. nix-shell throws

createdb: 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"?

My shell.nix content:

{ pkgs ? import <nixpkgs> {} }:
  pkgs.mkShell {
    # nativeBuildInputs is usually what you want -- tools you need to run
    nativeBuildInputs = [ 
    pkgs.buildPackages.scala_2_13 
    pkgs.buildPackages.postgresql_9_6
    pkgs.buildPackages.vim
    ];
  }

To start entering nix shell the command being ran is nix-shell --pure shell.nix. Searching the forum I don’t find the related solution. Any suggestions on how to fix or debug this issue?

I know I need to check if the server if up running or not. But executing pg_ctl -D .tmp/mydb -l logfile -o "--unix_socket_directories='$PWD'" start shows

pg_ctl: another server might be running; trying to start server anyway
server starting

And there are no ps, which utility. Maybe someone can tell me which base packages (Searching with nix base util keywords doesn’t find the solution either) I should install first? Thanks

[1]. Using PostgreSQL in a nix-shell | Michael Maclean

OK I find the root cause. It’s because the server is listening (domain socket) to --unix_socket_directories='$PWD' which is my current working directory (So the .s.PGSQL.5432 can be found at $PWD/.s.PGSQL.5432). However, the default createdb is trying to connect to the server through the domain socket at /run/postgresql/.s.PGSQL.5432 as the error message displayed. So switching the command from

createdb mydb 

to

createdb -h /tmp mydb # where -h specify the host but it can be local dir that contains the domain socket file.

fixes the problem. Thanks.