0

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

asked Oct 21, 2024 at 7:22
3
  • Yes, you can see that I have echoed the var before trying. Commented Oct 21, 2024 at 10:35
  • Rule #1 when asking for help: show your work. Commented Oct 21, 2024 at 11:30
  • @LaurenzAlbe You are right! After 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 with export PGUSER=$PGUSER! Commented Oct 22, 2024 at 11:40

1 Answer 1

2

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.)

answered Oct 22, 2024 at 13:54
3
  • 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!!! Commented 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 if PGUSER is already exported. Commented 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! Commented Oct 22, 2024 at 14:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.