tools

get_database_url

msdss_base_database.tools.get_database_url(driver='postgresql', user='msdss', password='msdss123', host='localhost', port='5432', database='msdss', load_env=False, env=<msdss_base_database.env.DatabaseDotEnv object>, *args, **kwargs)[source]

Form database connection url from parameters or an environmental variables file.

Parameters
  • driver (str) – The driver name of the database connection, which are commonly postgresql, sqlite, mysql, oracle or mssql (see SQLAlchemy supported databases).

  • user (str) – User name for the connection.

  • password (str) – Password for the user.

  • host (str) – Host address of the connection.

  • port (str) – Port number of the connection.

  • database (str) – Database name of the connection.

  • load_env (bool) – Whether to load the environmental variables using parameter env or not. The environment will only be loaded if the env_file exists.

  • env (msdss_base_database.env.DatabaseDotEnv) –

    An object to set environment variables related to database configuration. These environment variables will overwrite the parameters above if they exist.

    By default, the parameters above are assigned to each of the environment variables below:

    <parameter> = <environment variable>
    
    driver = MSDSS_DATABASE_DRIVER
    user = MSDSS_DATABASE_USER
    password = MSDSS_DATABASE_PASSWORD
    host = MSDSS_DATABASE_HOST
    port = MSDSS_DATABASE_PORT
    database = MSDSS_DATABASE_NAME
    

  • *args – Additional arguments passed to sqlalchemy.engine.URL.create.

  • **kwargs – Additional arguments passed to sqlalchemy.engine.URL.create.

Returns

String representing the database connection url.

Return type

str

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_base_database.tools import get_database_url

url = get_database_url(
    driver='postgresql',
    user='msdss',
    password='msdss12',
    port='5432',
    database='msdss'
)

print(url)
postgresql://msdss:msdss12@localhost:5432/msdss