如何在nixos中使用postgresql

Pie*_*aud 5 postgresql nixos nix

我已经安装了 PostgreSQL:

  nix-env -iA nixos.postgresql
Run Code Online (Sandbox Code Playgroud)

现在当我使用 时psql,我得到:

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?
Run Code Online (Sandbox Code Playgroud)

我必须添加一些东西/etc/nixos/configuration.nix吗?

更新

我尝试像此链接那样进行操作(即,将其安装在nix-shell而不是安装在中nix-env),但我仍然遇到相同的错误:

$ 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"?
Run Code Online (Sandbox Code Playgroud)

Din*_*har 5

psqlPostgreSQL 交互终端

您想要做的是在不首先运行 PostgreSQL 的情况下连接到数据库。

按着这些次序:

  1. 初始化数据库initdb -D .data
  2. 启动 PostgreSQL 服务器pg_ctl -D .data -l logfile start
  3. 确保它正在运行pg_ctl -D .data status
  4. 连接到数据库psql -d postgres默认情况下,创建 postgres 数据库

最后,确保pg_ctl -D .data stop在离开 nix shell 时停止数据库。