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
Related
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.
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
Assume we need to run second postgres instance in our production environment. First of them (postgres-one) already running and has few databases & data in them. Now I want to update my docker-compose.yaml file and add configuration for the second (postgres-two). Moreover I have to grab some databases info from postgres-one and copy it to postgres-two. Here how I'm trying to achieve this:
docker-compose.yaml
postgres-two:
image: postgres:12.5
depends_on:
postgres-one:
condition: service_started
...
ports:
- "5433:5432"
command: bash -c "chmod +x /usr/local/bin/init.sh && /usr/local/bin/init.sh"
volumes:
- ./data/postgres-two/init-db/init.sh:/usr/local/bin/init.sh
init.sh
#!/bin/bash
# allows you to skip the password prompt for pg_dump
echo "postgres-one:5432:dbname1_one:dbuser1_one:dbpass1_one" > ~/.pgpass
echo "postgres-one:5432:dbname2_one:dbuser2_one:dbpass2_one" >> ~/.pgpass
chmod 600 ~/.pgpass
# gets the data from external database & copies it to internal
pg_dump -h postgres-one -U dbuser1_one dbname1_one | psql -h localhost -U dbuser1_two -d dbname1_two
pg_dump -h postgres-one -U dbuser2_one dbname2_one| psql -h localhost -U dbuser2_two -d dbname2_two
But when I run this I get the error:
psql: error: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
I already tried it w/o -h localhost before, it gave me this as I remember:
psql: error: 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"?
If I add find /var/run/postgresql/ -name .s.PGSQL.5432 in the beginning of the init.sh it'll show nothing. So, as I understand I can't proceed further with psql because postgres server is not running at the moment. And of course I can't run it with postgres / pg_ctl commands because they can't be executed by root:
"root" execution of the PostgreSQL server is not permitted.
And of course docker containers run as the root user by default, and if I change the user it also give me errors such as:
chmod: changing permissions of '/usr/local/bin/init.sh': Operation not permitted
Am I doing something wrong? Or maybe I can get the dumps and apply them in other ways.. somehow?
I think server is failing to start because of volume mapping,
You can refer to this yml,
https://github.com/khezen/compose-postgres/blob/master/docker-compose.yml
Nvm, I solved the problem by placing the init.sh script into the docker-entrypoint-initdb.d folder. But then I also had an access issue with pg_dump because for some reason .pgpass didn't work in my case. For those, who will face the same problem, I fixed it with PGPASSWORD env like that:
PGPASSWORD="dbpass1_one" pg_dump -h postgres-one -U dbuser1_one dbname1_one | psql -h localhost -U dbuser1_two -d dbname1_two
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
When I typed psql in the terminal I get the below:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
My brew services list shows that Postgres is running:
postgresql#9.5 started john doe /Users/johndoe/Library/LaunchAgents/homebrew.mxcl.postgresql#9.5.plist
Run this to determine, where is your cluster data directory (the directory, where Postgres keeps data, WAL logs, etc):
sudo ps ax | grep postgres | grep D
If Postgres is running, you will see smth like this (this is Postgres.app, not Homebrew version, but it should be similar):
Nikolays-MacBook-Pro:~ nikolay$ sudo ps ax | grep postgres | grep D
Password:
599 ?? S 0:00.06 /Users/nikolay/PostgreSQL/pg96/bin/postgres -D /Users/nikolay/PostgreSQL/data/pg96 -r /Users/nikolay/PostgreSQL/data/logs/pg96/postgres.log
Go to the cluster directory ("/Users/nikolay/PostgreSQL/data/pg96" in this example) and open postgresql.conf, it should be there.
You need to find, what is set in the following options in your postgresql.conf:
listen_addresses
port
These two configurations tell you, on which network interfaces and which port Postgres is sitting.
In most cases, they are like this:
listen_addresses = '*'
port = 5432
There is additional config files, pg_hba.conf, you can consider it like an "internal firewall" for Postgres -- check rules, that are inside, and if needed, edit them. Remember, that if you edit any of connection settings, Postgres needs to be restarted.
In this case, to connect to Postgres, you need:
psql -h localhost -p 5432 -U postgres template1
Here I assumed that you installed Postgres under "postgres" OS user and during installation the corresponding database user was created. Probably you need to try different way:
psql -h localhost -p 5432 -U yourname template1
-- template1 is a default database which is a template for all DBs you will create, so you can try to connect to it anytime. "yourname" is your MacOS username.
Of course, "-p 5432" can be omitted because 5432 is Postgres' default port.
"-U yourname" can be also omitted -- in this case psql will take your OS username and use it like a DB username. And you can omit DB name also if it is the same as database username. So in some cases, "psql -h localhost" will work.
Hope it helps.