Subject: What are best practices wrt passwords?

From: [email-masked]
Date: Wed, 16 Oct 2024 11:35:46 +0200

Hello all,

I'd like to be able to use psql without typing passwords again and
again.  I know about `.pgpass` and PGPASSFILE, but I specifically do not
want to use it - I have the password in the `.env` file, and having it
in _two_ places comes with its own set of problems, like how to make
sure they don't get out of sync.

I understand why giving the password on the command line or in an
environment variable is a security risk (because of `ps`), but I do not
understand why `psql` doesn't have an option like `--password-command`
accepting a command which then prints the password on stdout.  For
example, I could then use `pass` (https://www.passwordstore.org/) with
gpg-agent.

Is there any risk associated with this usage pattern?  What is the
recommended practice in my case other than using `.pgpass`?

Thanks in advance,

P.S. Please CC me in replies, since I'm not subscribed to the list.
Thanks.

-- 
Marcin Borkowski
https://mbork.pl
https://crimsonelevendelightpetrichor.net/

---

From: Dominique Devienne <[email-masked]>
Date: Wed, 16 Oct 2024 14:41:47 +0200

> sure they don't get out of sync.

What's wrong with PGPASSWORD?
https://www.postgresql.org/docs/current/libpq-envars.html

> example, I could then use `pass` (https://www.passwordstore.org/) with

It's not psql, it's libpq, that does that, FTR.
My own apps are libpq based, and inherit all its env-vars and defaults.

But I'd welcome a way to store password encrypted,
unlike the current mechanisms. And what you propose
would allow that I guess, if I understand correctly. So +1.
(and since transient better than enrypted/obfuscated passwords)

> recommended practice in my case other than using `.pgpass`?

Storing password in plain text? --DD

---

From: [email-masked]
Date: Wed, 16 Oct 2024 14:16:45 +0000 (UTC)

Use the PGPASSWORD environment variable.
Example:

SET PGPASSWORD=P0stgres
psql -h localhost -p 5432 -U postgres -d postgres -c "SELECT 1;'"

 On Wednesday, October 16, 2024 at 08:26:05 AM GMT-4, <[email-masked]> wrote:

 Hello all,

Thanks in advance,

---

From: Alvaro Herrera <[email-masked]>
Date: Wed, 16 Oct 2024 16:37:11 +0200

> example, I could then use `pass` (https://www.passwordstore.org/) with

We had a patch to add PGPASSCOMMAND once:
https://www.postgresql.org/message-id/flat/CAE35ztOGZqgwae3mBA%3DL97pSg3kvin2xycQh%3Dir%3D5NiwCApiYQ%40mail.gmail.com

I don't remember the overall conclusions (other than the patch being
rejected), but maybe you can give that a read.

-- 
Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/