conninfo – manipulate connection strings

This module contains a few utility functions to manipulate database connection strings.

psycopg.conninfo.conninfo_to_dict(conninfo='', **kwargs)

Convert the conninfo string into a dictionary of parameters.

Parameters
  • conninfo (str) – A connection string as accepted by PostgreSQL.

  • kwargs (Any) – Parameters overriding the ones specified in conninfo.

Return type

Dict[str, Any]

Returns

Dictionary with the parameters parsed from conninfo and kwargs.

Raise ProgrammingError if conninfo is not a a valid connection string.

>>> conninfo_to_dict("postgres://jeff@example.com/db", user="piro")
{'user': 'piro', 'dbname': 'db', 'host': 'example.com'}
psycopg.conninfo.make_conninfo(conninfo='', **kwargs)

Merge a string and keyword params into a single conninfo string.

Parameters
  • conninfo (str) – A connection string as accepted by PostgreSQL.

  • kwargs (Any) – Parameters overriding the ones specified in conninfo.

Return type

str

Returns

A connection string valid for PostgreSQL, with the kwargs parameters merged.

Raise ProgrammingError if the input doesn’t make a valid conninfo string.

>>> make_conninfo("dbname=db user=jeff", user="piro", port=5432)
'dbname=db user=piro port=5432'