How do I connect to psql when it cannot connect to the server? - postgresql

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.

Related

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

Can't run script inside postgres container

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

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: Remotely connecting to Postgres instance using psql command

I want to remotely connect to a Postgres instance. I know we can do this using the psql command passing the hostname
I tried the following:
psql -U postgres -p 5432 -h hostname
I modified the /etc/postgresql/9.3/main/pg_hba.conf file on the target machine to allow remote connections by default
I added the following line to the file
host all all source_ip/32 trust
I restarted the cluster using
pg_ctlcluster 9.2 mycluster stop
pg_ctlcluster 9.2 mycluster start
However, when I try to connect from the source_ip, I still get the error
Is the server running on host "" and accepting TCP/IP connections on port 5432?
What am I doing wrong here?
I resolved this issue using below options:
Whitelist your DB host from your network team to make sure you have access to remote host
Install postgreSQL version 4 or above
Run below command:
psql -h <REMOTE HOST> -p <REMOTE PORT> -U <DB_USER> <DB_NAME>
psql -h <IP_Address> -p <port_no> -d <database_name> -U <DB_username> -W
-W option will prompt for password. For example:
psql -h 192.168.1.50 -p 5432 -d testdb -U testuser -W
I figured it out.
Had to set listen_addresses='*' in postgresql.conf to allow for incoming connections from any ip / all ip
Step Wise below
Opening the Port - Make sure the PSQL Port is open to all remote connections or connections from a specific set of IPs as per your requirement. PSQL, in general, runs at port 5432, and it is configurable, so expose relevant Port accordingly.
Update Remote Server PSQL Configuration - Set listen_addresses = '*' in postgresql.conf file, path in general is /etc/postgresql/psql_version/main/postgresql.conf
Connect remotely - psql -U <db_username> -h <IP_address> - in case psql is running on a port other than 5432 on the remote server, specify port by adding -p <port_number>
A little plus below -
In case the IP has been mapped to a domain name, you can connect by replacing <IP_address> with <host_name>. To do this, add a new connection rule in pg_hba.conf file
Note -
All above explained can cause security issues - best practice always is to either keep your psql port closed, or only allow a list of IPs to connect through the port.
Note that "ident" in pg_hba.conf requires a "ident server" to be running on the client.

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