Connect database#

metacatalog.api.connect_database(*args, **kwargs) Session#

Connect to database and returns a Session to the database.

You can either pass args and kwargs as accepted by sqlachemy’s create_engine method or pass a name of a stored connection string. Connection strings can be stored using save_connection method. Empty arguments will load the default connection string, if there is any.

Parameters:
  • *args – See sqlalchemy create_engine for a full list. The only additional argument is a stored connection string, if any. The function will first check for a stored connection of given name and only if none is found, pass *args down to create_engine. If len(args)==0, a stored connection of name 'default' will be loaded.

  • **kwargs – Only used if no stored connection is loaded.

See also

save_connection

Example#

Stored default connection#

[1]:
from metacatalog import api
session = api.connect_database()

print(session)
print(session.bind)
<sqlalchemy.orm.session.Session object at 0x7f97b40b0250>
Engine(postgresql://postgres:***@localhost:5432/metacatalog)

Connect without storing passwords#

In many productions scenarios you don’t want to store a password in cleartext to any kind of file, even not in a trusted environment. Usually, you will set the password as an environment variable. In Windows you do that like:

set POSTGRES_PASSWORD=notmyrealpassword

In Linux like:

export POSTGRES_PASSWORD=notmyrealpassword

In CIs like Travis CI, CirleCI or Github actions you can usually set them in the repositories settings.

Warning

If you are using a CI, remember to not set the password environment variable into any kind of yaml config files, which usually also define a section on envirnment variables. These yaml configs are often public!

[5]:
%env POSTGRES_PASSWORD=notmyrealpassword
env: POSTGRES_PASSWORD=notmyrealpassword

Then, istead of loading stored connection strings from the ~/.metacatalog/config.json, you can pass the connection URI directly to connect_database:

[6]:
import os
session = api.connect_database(
    'postgresql://dbuser:{pw}@localhost:5432/metacatalg'.format(pw=os.environ['POSTGRES_PASSWORD'])
)

print(session)
print(session.bind)
<sqlalchemy.orm.session.Session object at 0x7f9785755090>
Engine(postgresql://dbuser:***@localhost:5432/metacatalg)

Warning

Remember that IPython consoles cache all commands into a sqlite database. If you store the password first into a variable like:

password = 'notmyrealpassword'
api.connect_database('...'.format(pw=password))

Your password will be in the cache then!