For many years I have used to use the environment variable $PGUSER
when I work with client tools such as psql/pg_dump/pg_restore.
Recently I build up a new PG13 server, the env var $PGUSER
does NOT take effect, when I try to use the client tools on a terminal of the server.
I get message like this:
leon@mamba:~$ echo $PGUSER
postgres
leon@mamba:~$ psql
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed:
FATAL: role "leon" does not exist
If I use the tools on a terminal of another physical computer, the env var takes effect normally.
Of course, the var has been set on both machines.
My pg_hba.conf:
# TYPE DATABASE USER ADDRESS METHOD
host all all 127.0.0.1/32 trust
host all all 192.168.x.x/16 md5
# IPv6 local connections:
host all all ::1/128 trust
1 Answer 1
It is not enough to set a variable, you also have to export
it to the environment of the processes you start.
If you set a variable with
PGUSER=postgres
it will be set in the environment of your shell process, but the processes you start won't inherit the variable. You have to do it as follows if you want the variable set in the environment of the psql
process you are starting:
export PGUSER=postgres
(This answer applies to the Bourne shell, the Korn shell, bash
and similar shells. Other shells like the C shell have different ways of exporting variables.)
-
I can get it on my PC without
export
, but can NOT on my new server. The way I do things are same. Thank you anyway!!!Leon– Leon2024年10月22日 14:04:12 +00:00Commented Oct 22, 2024 at 14:04 -
With PC, do you mean Windows? That may work differently. Other than that, perhaps someone already exported the variable for you, and you only change the value. You can run
export
without arguments to see ifPGUSER
is already exported.Laurenz Albe– Laurenz Albe2024年10月22日 14:10:22 +00:00Commented Oct 22, 2024 at 14:10 -
My PC and the server are all debian12. The only thing I did before on both sides, is appending a line of
PGUSER=postgres
into /etc/environment.d/95-postgresql.conf. But it takes effects only on the PC. A difference is that I'm using psql in a Konsole of KDE on PC, while on the server, I do it through ssh. Another diff is that PC is Debian 12.5, while the server is Debian 12.7. Thank you again!Leon– Leon2024年10月22日 14:30:00 +00:00Commented Oct 22, 2024 at 14:30
Explore related questions
See similar questions with these tags.
export PGUSER=postgres
, I successfully used client tools without specifying CLI arguments. Would you please to explain why I got this? What's the difference between the env var assigned by CLI and by /etc/environment file? I can even get it withexport PGUSER=$PGUSER
!