How to use postgresql in nixos - postgresql

I've installed posgresql:
nix-env -iA nixos.postgresql
Now I wanto use it.
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?
Must I add something in /etc/nixos/configuration.nix ?
update
I've tried to do like this link. i.e. install in nix-shell and not in nix-env.
I've the same shell.nix
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"?

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:
Initialize the database with initdb -D .data
Start a PostgreSQL server with pg_ctl -D .data -l logfile start
Make sure it's running pg_ctl -D .data status
Connect to database with 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.

Related

Postgresql not starting even after editing rc file

I receive the following error when I try to start postgresql aka psql in Slackware Linux:
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
I am trying to start it and changed the field in pg_hba.conf to trust from md5.
At some point it was asking for a password.
I get the following when I try to start the server:
bash-5.1# postgres -D /pgsql/data
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise. See the documentation for
more information on how to properly start the server.
bash-5.1# postgres -D /pgsql/data >logfile 2>&1 &
[1] 8310

Connect to postgres with custom database cluster location with psql

Instead of using /usr/local/pgsql/data, I create my database cluster with the following code
initdb -D /tmp/psql
pg_ctl -D /tmp/psql -l logfile -o "--unix_socket_directories='$PWD'" start
But when I run psql, I get the following error.
psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
I have read the doc and I cannot find related options for changing the location for database cluster. What have I missed?
I ran into the same issue and specifying the socket directory with the -h option fixes the issue.
psql -h $PWD -p 5432 postgres

Sqitch cannot connect to a running Postgres instance

I have a Postgres 9.6 instance on OSX that is up and running, but Sqitch throws the following error when I try sqitch status in a working directory with a sqitch.conf:
$ sqitch status
# On database db:pg:my_db
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
This is odd because I have already checked that Postgres is running by checking its status and logging in directly:
$ pg_isready
/tmp:5432 - accepting connections
$ psql -U postgres
psql (9.6.19)
Type "help" for help.
postgres=#
This seems to be just a problem with sqitch.
For more detail, this Postgres was installed via brew install postgresql#9.6 and is located in the default directory:
$ which psql
/usr/local/opt/postgresql#9.6/bin/psql
Regarding Sqitch, I have tried both installing with Homebrew and using Docker (my current approach). The docker install is based on the official instructions:
docker pull sqitch/sqitch
curl -L https://git.io/JJKCn -o sqitch && chmod +x sqitch
./sqitch status
I tried setting psql explicitly as well with sqitch config --user engine.pg.client /usr/local/opt/postgresql#9.6/bin/psql
Regardless, I still get the following with any sqitch command:
$ sqitch status
# On database db:pg:my_db
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I'm not sure what I'm missing and could use some input. Thanks in advance.
I don't know what's up with the Brew-installed Sqitch, but when you run it from the Docker image, that container does not have Postgres running inside it, so connections to localhost will fail. You instead need to connect to Postgres outside the container. If you're running it on your Mac, this is straightforward to do: Make sure Postgres is listening on the IP ports, not just a Unix domain socket, and specify host.docker.internal as the host name, like so:
sqitch status db:pg://host.docker.internal/my_db

psql cannot connect without -h flag

I am running psql from a Debian 8.7 terminal but cannot figure out why it needs the -h flag to execute. For example, if i run psql -U postgres on the terminal, I get the following error:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
However, when I add the host flag, I am able to connect:
psql -U postgres -h localhost
psql (9.4.10, server 9.6.2)
WARNING: psql major version 9.4, server major version 9.6.
Some psql features might not work.
Type "help" for help.
postgres=#
What is the cause?
It looks like the client and the server have a different idea about the directory where UNIX sockets should be created.
While connected to the database, run
SHOW unix_socket_directories;
That will tell you in which directory (or directories) the UNIX socket can be found.
You can use the directory name with the -h option or set the environment variable PGHOST to it for a local connection.
For example, if the result you get is /tmp (the factory default), you can use
psql -h /tmp -U postgres

Postgresql (pgsql) client expecting sockets in different location from postgrsql-server

While trying to connect to postgres running locally on my workstation, I get:
$ sudo -u postgres psql -c "create role ..."
could not change directory to "/home/esauer/workspace/cfme"
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
My postgres-server install creates sockets in /var/run/postgresql.
How do I get the client to look in the proper location?
Check the --host option with psql --help.
Then you can make it permanent by setting it in your .psqlrc user file.
In your case try:
psql -h /var/run/postgresql -d your_database