question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New parser: PostgreSQL Password File

See original GitHub issue

Being able to read the PostgreSQL Password File as JSON would be handy and it is a very simple format:

The file .pgpass in a user’s home directory… should contain lines of the following format:

hostname:port:database:username:password

Each of the first four fields can be a literal value, or *, which matches anything. The password field from the first line that matches the current connection parameters will be used. (Therefore, put more-specific entries first when you are using wildcards.) If an entry needs to contain : or , escape this character with .

So cat /var/lib/postgresql/.pgpass | jc -yp --pgpass could perhaps return something like:

---
- hostname: localhost
  port: 5432
  database: foo
  username: bar
  password: baz

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kellyjonbrazilcommented, Dec 5, 2022

Hi @chriscroome - I have a working version in dev now: https://github.com/kellyjonbrazil/jc/blob/dev/jc/parsers/pgpass.py

$ cat pgpass.txt                
dbserver:443:db1:dbuser:pwd123
dbserver2:8888:inventory:joeuser:abc123
ax1234:1234:tables:dbadmin:password1

dbserver3:8888:inventory:joeuser:abc123
# comment

localhost:5555:table\\1:user\:w\:colon:password123
hostname:port:database:username:password

$ cat pgpass.txt| jc --pgpass -p                         
[
  {
    "hostname": "dbserver",
    "port": "443",
    "database": "db1",
    "username": "dbuser",
    "password": "pwd123"
  },
  {
    "hostname": "dbserver2",
    "port": "8888",
    "database": "inventory",
    "username": "joeuser",
    "password": "abc123"
  },
  {
    "hostname": "ax1234",
    "port": "1234",
    "database": "tables",
    "username": "dbadmin",
    "password": "password1"
  },
  {
    "hostname": "dbserver3",
    "port": "8888",
    "database": "inventory",
    "username": "joeuser",
    "password": "abc123"
  },
  {
    "hostname": "localhost",
    "port": "5555",
    "database": "table\\1",
    "username": "user:w:colon",
    "password": "password123"
  },
  {
    "hostname": "hostname",
    "port": "port",
    "database": "database",
    "username": "username",
    "password": "password"
  }
]

It can handle commented lines and also escaped \ and : characters. Should the port field be an integer? (Actually, probably not since the field can be filled with an * wildcard)

1reaction
kellyjonbrazilcommented, Nov 30, 2022

Looks good - I’ll start working on some new parsers for the next release soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: 34.16. The Password File - PostgreSQL
The file .pgpass in a user's home directory can contain passwords to be used if the connection requires a password (and no password...
Read more >
pgpasslib — pgpasslib 1.1.0 documentation
pgpasslib is a library for retrieving passwords from a PostgreSQL password file, either from a location specified in the PGPASSFILE environment variable or ......
Read more >
gsiems/orapass: An Oracle password file parser ... - GitHub
An Oracle password file parsing library and utility based on the .pgpass file available to PostgreSQL users, orapass searches a similarly formatted file...
Read more >
How do I specify a password to 'psql' non-interactively?
pgpass file that your bash script has access to. Or don't use passwords at all--you could set up another form of authentication, such...
Read more >
Password file paths as libpq connection parameter
Postgres 10 highlight - Password file paths as libpq connection ... now it can also be selected via a new connection parameter "passfile", ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found