I am interested in using the sqlalchemy.engine.URL.create constructor, but I am finding the documentation to be a too brief on the details to understand how to use it.
Here are the parameters:
drivername: the name of the database backend. This name will correspond to a module in sqlalchemy/databases or a third party plug-in.username: The user namepassword: database password. Is typically a string, but may also be an object that can be stringified withstr().host: The name of the hostport: The port numberdatabase: The database namequery: A dictionary of string keys to string values to be passed to the dialect and/or the DBAPI upon connect. To specify non-string parameters to a Python DBAPI directly, use thecreate_engine.connect_argsparameter tocreate_engine(). See alsoURL.normalized_queryfor a dictionary that is consistently string->list of string.
The parameter I am confused about is query. I don't know how to use it for PostgreSQL. I found an example that works for Microsoft SQL:
from sqlalchemy.engine import URL
from sqlalchemy import create_engine
# CONFIG
SERVER_NAME = 'FOO'
DATABASE_NAME = 'BAR'
# QUERY
QUERY = '''SELECT * FROM table'''
# CONNECTION
CONNECTION_STRING = 'Driver={SQL Server};Server={%s};Database=%s;Trusted_Connection=yes;' % (
SERVER_NAME,
DATABASE_NAME
)
CONNECTION_URL = URL.create("mssql+pyodbc",
query={"odbc_connect": CONNECTION_STRING})
CONNECTION = create_engine(CONNECTION_URL)
Which makes it evident that I can use it for passing in the connection string. I think I can replace "mssql+pyodbc" with "postgresql+psycopg2", but I don't know what to replace "odbc_connect" with.
- (short-term) What can I put into
queryto change to PostgreSQL and - (more fundamentally) where can I find detailed documentation for understanding the possible inputs of the
queryparameter?