I’m trying to change default port and host for psql. I added PGHOST and PGPORT to /etc/bash.bashrc and I can echo them and ping my remote db_host
server:
$ echo $PGHOST
db_host
$ echo $PGPORT
19000
$ ping db_host
PING db_host (172.16.2.160) 56(84) bytes of data.
64 bytes from db_host (172.16.2.160): icmp_seq=1 ttl=64 time=0.565 ms
64 bytes from db_host (172.16.2.160): icmp_seq=2 ttl=64 time=0.925 ms
However, if I run psql I get a could not connect to server
error.
$ psql
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"?
Psql is trying to connect to my local instance and ignores PGHOST and PGPORT. Why doesn’t psql pick up the variables?
If I specify port and host in command line like psql -h $PGHOST -p $PGPORT
, psql can connect to my remote server.
P.S. I have Ubuntu Server 22.04 and Postgres 13 on local machine and on the remote side.
2
Answers
Finally I found the problem. My
psql
command was added to /etc/super.tab. As soon it was removed,psql
recognizes my env variable.This is a shell problem, not a database problem: you set the variables, but didn’t
export
them so that they get passed to the environment of thepsql
process you start. Trybefore calling
psql
.