Well – 2nd thought (June 2023) this may work but if you are on AWS with Redshift or PostgreSQL AWS Secrets Manager is more secure with a little monthly billing ;-)… as the downside. AWS parameter store will also work and it is free but not much more secure than below – if any.
touch $HOME/.pgpass
chmod 600 $HOME/.pgpass
Contents of the file .pgpass will be in the format:
hostname
:port
:database
:username
:password