I’ve installed PostgreSQL:
nix-env -iA nixos.postgresql
Now when I use psql
, I get:
psql: error: connection to server on socket "/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
Must I add something in /etc/nixos/configuration.nix
?
update
I’ve tried to do like this link (i.e., to install it in nix-shell
and not in nix-env
), but I still get the same error:
$ nix-shell --pure shell.nix
done
server started
$ psql
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/mnt/c/Users/Pierre-Olivier/nix/psql/.pg/.s.PGSQL.5432"?
2
Answers
psql
is the PostgreSQL interactive terminal.What you are trying to do is connect to database without PostgreSQL running in first place.
Follow these steps:
initdb -D .data
pg_ctl -D .data -l logfile start
pg_ctl -D .data status
psql -d postgres
(by default, postgres database is created)At the end, make sure to stop database with
pg_ctl -D .data stop
when leaving the nix shell.1.
nix-env -iA nixos.postgresql
psql
won’t connect afternix-env
because it just puts the PostgreSQL package into the/nix/store
, but doesn’t start it. You can check it on the terminal with2.
nix-shell --pure
shell.nix
This puts the PostgreSQL package in the
/nix/store
, starts and initializes the database cluster (withpg_ctl start
andinitdb
, respectively). The relevant lines in theshell.nix
:So, in this case, the PostgreSQL instance is running, but you’ll need to specify a couple of things, such as
--host=
("_ the host name of the machine on which the server is running_") and--dbname
(the name of the database in the cluster you would like to connect):By default, there are only the following databases present in a newly created PostgreSQL cluster:
My constant companion is the PostgreSQL documentation; some of the relevant pages:
initdb
man pagepg_ctl
man pagepsql
man pagePGDATA
The replies to the How to configure PostgreSQL declaratively (NixOS and non-NixOS)? NixOS forum thread also helped me to get started. (There are fairly advanced examples in there that I’m still trying to understand.)
A basic (and insecure)
shell.nix
example that I use for development is this (from here; fine-tuned a bit since then, but the gist is the same):