Postgres¶
Postgres is popular open source SQL database dialect. The Parsons class leverages the psycopg2 python package.
Quickstart¶
from parsons import Postgres
pg = Postgres()
from parsons import Postgres
pg = Postgres(username='me', password='secret', host='mydb.com', db='dev', port=3306)
from parsons import Postgres
pg = Postgres()
tbl = pg.query('select * from my_schema.secret_sauce')
tbl = Table.from_csv('my_file.csv') # Load from a CSV or other source.
pg.copy(tbl, 'my_schema.winning_formula')
API¶
- class parsons.databases.postgres.postgres.Postgres(username=None, password=None, host=None, db=None, port=None, timeout=10)[source]¶
A Postgres class to connect to database. Credentials can be passed from a
.pgpassfile stored in your home directory or with environmental variables.- Parameters:
username – str Required if env variable
PGUSERnot populatedpassword – str Required if env variable
PGPASSWORDnot populatedhost – str Required if env variable
PGHOSTnot populateddb – str Required if env variable
PGDATABASEnot populatedport – int If omitted or
None, usesPGPORTwhen set, otherwise 5432. If passed (including5432), the argument takes precedence overPGPORT.timeout – int Seconds to timeout if connection not established.
- copy(tbl: Table, table_name: str, if_exists: Literal['fail', 'append', 'drop', 'truncate'] = 'fail', strict_length: bool = False)[source]¶
Copy a Table to Postgres.
- Parameters:
tbl (Table) – Table A Parsons table object
table_name (str) – str The destination schema and table (e.g.
my_schema.my_table)if_exists (Literal['fail', 'append', 'drop', 'truncate']) – str If the table already exists, either
fail,append,droportruncatethe table.strict_length (bool) – bool If the database table needs to be created, strict_length determines whether the created table’s column sizes will be sized to exactly fit the current data, or if their size will be rounded up to account for future values being larger then the current dataset. Defaults to
False.
- connection()¶
Generate a Postgres connection. The connection is set up as a python “context manager”, so it will be closed automatically (and all queries committed) when the connection goes out of scope.
When using the connection, make sure to put it in a
withblock (necessary for any context manager):with pg.connection() as conn:- Yields:
Psycopg2 connection object
- create_table(table_object, table_name)¶
Create a table based on table object data.
- detect_data_type(value, cmp_type=None)¶
Detect the higher of value’s type cmp_type.
check if it’s a string
check if it’s a number
check if it’s a float
check if it’s an int
- Parameters:
value – str The value to inspect.
cmp_type – str The string representation of a type to compare with the type of
value.
- Returns:
- str
String representation of the higher of the two types.
- format_column(col: str, index: int | str = '', replace_chars: dict | None = None, col_prefix: str = '_') str¶
Format the column to meet database contraints.
Formats the columns as follows: 1. Coverts to lowercase (if case insensitive) 2. Strips leading and trailing whitespace 3. Replaces invalid characters 4. Renames if in reserved words
- Parameters:
col (str) – The column to format.
index (int | str) – The index of the column. Used if the column is empty.
replace_chars (dict | None) – A dictionary of invalid characters and their replacements. If
Noneuses {” “: “_”}col_prefix (str) – The prefix to use when the column is empty or starts with an invalid character.
- Returns:
The formatted column.
- Return type:
- format_columns(cols, **kwargs)¶
Format the columns to meet database contraints.
This method relies on
format_columnto handle most changes. It only handles duplicated columns. Options toformat_columncan be passed through kwargs.- Parameters:
cols – list The columns to format.
**kwargs – dicts Keyword arguments to pass to
format_column.
- Returns:
- list
The formatted columns.
- generate_alchemy_url()¶
Generate a SQL Alchemy engine https://docs.sqlalchemy.org/en/14/core/engines.html#
- generate_engine()¶
Generate a SQL Alchemy engine.
- get_bigger_int(int1, int2)¶
Return the bigger of the two ints.
- Parameters:
int1 – str The string representation if an int type.
int2 – str The string representation if an int type.
- Returns:
- str
A string representation of the higher of the two int types.
- get_table_object(table_name)¶
Get a SQL Alchemy table object.
- is_valid_sql_num(val)¶
Check whether val is a valid sql number.
- Parameters:
val – any The values to check.
- Returns:
- bool
Whether or not the value is a valid sql number.
- query(sql: str, parameters: list | None = None) Table | None¶
Execute a query against the database. Will return
Noneif the query returns zero rows.To include python variables in your query, it is recommended to pass them as parameters, following the psycopg style. Using the
parametersargument ensures that values are escaped properly, and avoids SQL injection attacks.Parameter Examples
# Note that the name contains a quote, which could break your query if not escaped # properly. name = "Beatrice O'Brady" sql = "SELECT * FROM my_table WHERE name = %s" rs.query(sql, parameters=[name])
names = ["Allen Smith", "Beatrice O'Brady", "Cathy Thompson"] placeholders = ', '.join('%s' for item in names) sql = f"SELECT * FROM my_table WHERE name IN ({placeholders})" rs.query(sql, parameters=names)
- query_with_connection(sql, connection, parameters=None, commit=True)¶
Execute a query against the database, with an existing connection. Useful for batching queries together. Will return
Noneif the query returns zero rows.- Parameters:
sql – str A valid SQL statement
connection – obj A connection object obtained from
redshift.connection()parameters – list A list of python variables to be converted into SQL values in your query
commit – boolean Whether to commit the transaction immediately. If
Falsethe transaction will be committed when the connection goes out of scope and is closed (or you can commit manually withconnection.commit()).
- Returns:
- Table
See Table for output options.
- static split_table_name(full_table_name: str) tuple[str, str] | None¶
Parse the schema and table name.