pgpasslib

pgpasslib is a library for retrieving passwords from a PostgreSQL password file, either from a location specified in the PGPASSFILE environment variable or in the .pgpass file in the current user’s home directory.

Version Downloads

Installation

pgpasslib may be installed via the Python package index with the tool of your choice. I prefer pip:

pip install pgpasslib

Requirements

There are no requirements outside of the Python standard library.

API Documentation

pgpasslib.getpass() will attempt to return the password for the specified host, port, database name, and username from the PostgreSQL Password file.

The password file from the .pgpass file in the current user’s home directory or as specified by the PGPASSFILE environment variable or on Windows the pgpass.conf file in the %APPDATA%\postgresql folder.

Example:

import pgpasslib

password = pgpasslib.getpass('localhost', 5432, 'postgres', 'postgres')
if not password:
    raise ValueError('Did not find a password in the .pgpass file')
exception pgpasslib.FileNotFound

Raised when the password file specified in the PGPASSFILE environment variable or .pgpass file in the user’s home directory does not exist.

exception pgpasslib.InvalidEntry

Raised when the password file can not be parsed properly due to errors in the entry format.

exception pgpasslib.InvalidPermissions

Raised when the password file specified in the PGPASSFILE environment variable or .pgpass file in the user’s home directory has group or world readable permission bits set.

exception pgpasslib.PgPassException

Base exception for all pgpasslib exceptions

pgpasslib.getpass(host='localhost', port=5432, dbname='docs', user='docs')

Return the password for the specified host, port, dbname and user. None will be returned if a password can not be found for the specified connection parameters.

If the password file can not be located, a FileNotFound exception will be raised.

If the password file is group or world readable, the file will not be read, per the specification, and a InvalidPermissions exception will be raised.

If an entry in the password file is not parsable, a InvalidPermissions exception will be raised.

Parameters:
  • host (str) – PostgreSQL hostname
  • port (int or str) – PostgreSQL port
  • dbname (str) – Database name
  • user (str) – Database role/user
Return type:

str

Raises:

FileNotFound

Raises:

InvalidPermissions

Raises:

InvalidEntry

Version History

See Version History

Issues

Please report any issues to the Github project at https://github.com/gmr/pgpasslib/issues

Source

pgpasslib source is available on Github at https://github.com/gmr/pgpasslib

License

pgpasslib is released under the 3-Clause BSD license.

Indices and tables