Using environment variables

One way to pass all kinds of parameters is to use environment variables. If information is not explicitly passed to pg_dump, it will look for the missing information in predefined environment variables. A list of all potential settings can be found at https://www.postgresql.org/docs/11/static/libpq-envars.html.

The following overview shows some of the environment variables that are commonly needed for backups:

  • PGHOST: This tells the system which host to connect to.
  • PGPORT: This defines the TCP port to be used.
  • PGUSER: This tells a client program about the desired user.
  • PGPASSWORD: This contains the password to be used.
  • PGDATABASE: This is the name of the database to connect to.

The advantage of these environments is that the password won't show up in the process table. However, there's more. Consider the following example:

psql  -U ... -h ... -p ... -d ... 

Given that you are a system administrator, would you really want to type a long piece of code such as this a couple of times every day? If you are working with the very same host again and again, just set those environment variables and connect with plain SQL. The following listing shows how to connect:

[hs@linuxpc ~]$ export PGHOST=localhost
[hs@linuxpc ~]$ export PGUSER=hs
[hs@linuxpc ~]$ export PGPASSWORD=abc
[hs@linuxpc ~]$ export PGPORT=5432
[hs@linuxpc ~]$ export PGDATABASE=test
[hs@linuxpc ~]$ psql
psql (11.0)
Type "help" for help.  

As you can see, there are no command-line parameters anymore. Just type psql and you are in.

All applications based on the standard PostgreSQL C language client library (libpq) will understand these environment variables, so you can use them not only for psql and pg_dump, but for many other applications.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset