Postgresql – user mapping hide password

foreign-datapostgresql

I would like to perform queries over different postgresql11-databases. Therefore I created a data wrapper and a user mapping as following:

CREATE server 'test' foreign data wrapper postgres_fdw
OPTIONS (dbname 'name_db', host 'ip_adress');

CREATE USER MAPPING FOR "Userame"
SERVER 'test' OPTIONS (user 'Username', password '******');

Afterwards it is possible to query with db_link left join etc.. so far no problem. My question is now, how can I hide the passwords for the different users that are visible in foreign data wrappers-postgres_fdw-'test'-user mapping-options ???

Best Answer

Create a .pgpass file on the postgres server in the $HOME directory of the postgres Linux user. You can then store the password of that database user in that file.

Then you can remove the password option from the user mapping, as Postgres (or more precisely: libpq) will pick up the password from the password file.

Don't forget to set password_required to 'false' on the user mapping unless the user mapping is for a superuser (which is an altogether awful thing to do).