A Python/CLI tool for:
- Exporting database tables to compressed CSV files.
- Transferring tables from from one database server to another.
- Loading database data (from both files and Python)
- Creating/Managing Postgresql/TimescaleDB tables, views, materialized views, functions, procedures, continuous aggregates, scheduled tasks.
- Checking for mismatched attributes between SQLAlchemy tables/models and actual tables in a database.
Currently only Postgresql and Postgresql-based databases (e.g. TimescaleDB) are supported.
Install
pip install dbflows
If using the export functionality (export database tables to compressed CSV files), then you will additionally need to have the psql executable available.
To install psql:
# enable PostgreSQL package repository
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo tee /etc/apt/trusted.gpg.d/pgdg.asc &>/dev/null
# replace `16` with the major version of your database
sudo apt update && sudo apt install -y postgresql-client-16
Export
Features:
- File splitting. Create separate export files based on a 'slice column' (an orderable column. e.g. datetime, integer, etc) and/or 'partition column' (a categorical column. e.g. name string).
- Incremental exports (export only data not yet exported). This works for both single file and multiple/split file output.
Examples
from dbflows import export
import sqlalchemy as sa
# the table to export data from
my_table = sa.Table(
"my_table",
sa.MetaData(schema="my_schema"),
sa.Column("inserted", sa.DateTime),
sa.Column("category", sa.String),
sa.Column("value", sa.Float),
)
# one or more save locations (2 in this case)
save_locs = ["s3://my-bucket/my_table_exports", "/path/to/local_dir/my_table_exports"]
# database URL
url = "postgres://user:password@hostname:port/database-name"
Export entire table to a single file.
export(
table=my_table,
engine=url, # or sa.engine
save_locs=save_locs
)
CLI equivalent:
db export table \
my_table.my_schema \
postgres://user:password@hostname:port/database-name` \
s3://my-bucket/my_table_exports \
/path/to/local_dir/my_table_exports
Export 500 MB CSVs, sorted and sliced on inserted datetime column.
export(
table=my_table,
engine=url, # or sa.engine
save_locs=save_locs,
slice_column=my_table.c.inserted,
file_max_size="500 MB"
)
Create a CSV export for each unique category in table.
export(
table=my_table,
engine=url, # or sa.engine
save_locs=save_locs,
partition_column=my_table.c.category
)
CLI equivalent:
db export table \
my_table.my_schema \
postgres://user:password@hostname:port/database-name` \
# save to one or more locations (s3 paths or local)
s3://my-bucket/my_table_exports \
/path/to/local_dir/my_table_exports \
--partition-column category # or "-p category"
export 500 MB CSVs for each unique category, sorted and sliced on inserted datetime column.
export(
table=my_table,
engine=url, # or sa.engine
save_locs=save_locs,
slice_column=my_table.c.inserted,
file_max_size="500 MB",
partition_column=my_table.c.category,
)
Loading/Importing
Loading from Python objects
Create a PgLoader instance for your table and use the load method to load batches of rows.
Loading from CSV files
Use import_csvs to load CSV with parallel worker threads. This internally uses timescaledb-parallel-copy which can be installed with: go install github.com/timescale/timescaledb-parallel-copy/cmd/timescaledb-parallel-copy@latest
Metadata
Release files for dbflows 0.2.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dbflows-0.2.6-py3-none-any.whl | Python 3 | none | any | Details for dbflows-0.2.6-py3-none-any.whl |
Release files / dbflows-0.2.6-py3-none-any.whl
| Download URL | dbflows-0.2.6-py3-none-any.whl |
|---|---|
| Size | 57.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2ffdbd52baeb314cee603aa5bc4e5cb45882bc1405df1bc7b82959e991a32eb7
|
|
BLAKE2b-256 checksum How to use checksums |
5558b92c74c54a214786425faeb893bf206d6a76d3166c7eaa503fb3186fb2e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.12
|
Security for dbflows 0.2.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.