make_database_address

pyhelpers.dbms.utils.make_database_address(host, port, username, database_name='')[source]

Generate a string representing a database address.

Parameters:
  • host (str) – Host name or IP address of the database server.

  • port (int | str) – Port number on which the database server is listening.

  • username (str) – Username used to connect to the database server.

  • database_name (str | None) – Name of the PostgreSQL database; defaults to an empty string.

Returns:

Address of the database in the format ‘<username>@<host>:<port>/<database_name>’.

Return type:

str

Examples:

>>> from pyhelpers.dbms.utils import make_database_address
>>> make_database_address('localhost', 5432, 'postgres', 'postgres')
'postgres:***@localhost:5432/postgres'
>>> make_database_address('localhost', 5432, 'admin', 'my_database')
'admin:***@localhost:5432/my_database'
>>> make_database_address('127.0.0.1', '5432', 'user', '')
'user:***@127.0.0.1:5432'