Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

sqlalchemy-redshift

Amazon Redshift dialect for SQLAlchemy.

Installation

The package is available on PyPI:

pip install sqlalchemy-redshift

Usage

The DSN format is similar to that of regular Postgres:

>>> import sqlalchemy as sa
>>> sa.create_engine('redshift+psycopg2://username@host.amazonaws.com:5439/database')
Engine(redshift+psycopg2://username@host.amazonaws.com:5439/database)

Three drivers are supported:

# psycopg2 (default)
engine = sa.create_engine('redshift+psycopg2://user:pass@host:5439/db')

# psycopg2cffi (PyPy compatible)
engine = sa.create_engine('redshift+psycopg2cffi://user:pass@host:5439/db')

# redshift_connector (Amazon's native driver)
engine = sa.create_engine('redshift+redshift_connector://user:pass@host:5439/db')

See the RedshiftDDLCompiler documentation for details on Redshift-specific features the dialect supports.

Materialized Views

Create and manage materialized views:

from sqlalchemy_redshift.ddl import (
    CreateMaterializedView, DropMaterializedView, RefreshMaterializedView
)

create = CreateMaterializedView('my_view', selectable, distkey='id')
refresh = RefreshMaterializedView('my_view')
drop = DropMaterializedView('my_view', if_exists=True)

Requirements

  • Python 3.10+

  • SQLAlchemy >= 1.4.0, < 3

  • One of: psycopg2, psycopg2-binary, psycopg2cffi, or redshift_connector

Running Tests

Tests are run via tox and can be run with the following command:

$ tox

You can also run tests directly with pytest:

$ pip install -e .
$ pytest tests/ --dbdriver psycopg2 -v

To run integration tests, set the following environment variables:

  • REDSHIFT_HOST

  • REDSHIFT_PORT

  • REDSHIFT_USERNAME

  • PGPASSWORD (this is the redshift instance password)

  • REDSHIFT_DATABASE

  • REDSHIFT_IAM_ROLE_ARN

Note that the IAM role specified will need to be associated with the Redshift cluster and have the correct permissions to create databases and tables as well as drop them. Practice caution when running these tests against a production instance.

Development

Pre-commit hooks are configured for this project:

$ pip install pre-commit
$ pre-commit install

Continuous Integration (CI)

Tests are run automatically via GitHub Actions on every push and pull request. The CI matrix tests across Python 3.10, 3.11, and 3.12 with both SQLAlchemy 1.4 and 2.0.

Releasing

To perform a release, you will need to be an admin for the project on GitHub and on PyPI. Contact the maintainers if you need that access.

You will need to have a ~/.pypirc with your PyPI credentials and also the following settings:

[zest.releaser]
create-wheels = yes

To perform a release, run the following:

python -m venv ~/.virtualenvs/dist
workon dist
pip install -U pip setuptools wheel
pip install -U tox zest.releaser
fullrelease  # follow prompts, use semver ish with versions.

The releaser will handle updating version data on the package and in CHANGES.rst along with tagging the repo and uploading to PyPI.

0.8.15 (unreleased)

  • Nothing changed yet.

0.8.14 (2023-04-07)

  • Override new upstream postgres method that fails against redshift (Pull #266)

  • Fix table reflection broken for non-superusers (Pull #276)

  • Fix Broken Reflection for 1.4 FutureEngine (Pull #277)

0.8.13 (2023-03-28)

  • Add spectrum support (Pull #263)

  • Drop support for Python 3.5

0.8.12 (2022-12-08)

  • Fix SQLAlchemy’s “supports_statement_cache” (Pull #259)

0.8.11 (2022-07-27)

  • Disable redshift_connector dialect statement cache (Pull #257)

0.8.10 (2022-07-21)

  • Support HLLSKETCH Redshift datatypes (Pull #246)

  • Disable supports_statement_cache (Pull #249)

  • Fix doc, lint CI dependency issues (Pull #250)

  • Fix redshift_connector dialect column encoding (Pull #255)

0.8.9 (2021-12-15)

  • Support inspection of Redshift datatypes (Pull #242)

0.8.8 (2021-11-03)

  • Remove support for Python 2.7; now requires python >=3.4 (Pull #234)

  • Support GEOMETRY, SUPER Redshift datatypes (Pull #235)

0.8.7 (2021-10-27)

  • Initial SQLAlchemy 2.0.x support (Pull #237)

0.8.6 (2021-09-22)

  • Add RedshiftDialect_redshift_connector (Pull #232)

  • Create RedshiftDialectMixin class. Add RedshiftDialect_psycopg2cffi. (Pull #231)

0.8.5 (2021-08-23)

0.8.4 (2021-07-15)

  • Improve reflection performance by fetching/caching metadata per schema rather than for the entire database (Pull #223)

0.8.3 (2021-07-07)

0.8.2 (2021-01-08)

  • Allow supplying multiple role ARNs in COPY and UNLOAD commands. This allows the first role to assume other roles as explained here.

0.8.1 (2020-07-15)

  • Support AWS partitions for role-based access control in COPY and UNLOAD commands. This allows these commands to be used, e.g. in GovCloud.

0.8.0 (2020-06-30)

  • Add option to drop materialized view with CASCADE (Pull #204)

  • Fix invalid SQLAlchemy version comparison (Pull #206)

0.7.9 (2020-05-29)

  • Fix for supporting SQLAlchemy 1.3.11+ (Issue #195)

0.7.8 (2020-05-27)

  • Added support for materialized views (Issue #202)

  • Fix reflection of unique constraints (Issue #199)

  • Support for altering column comments in Alembic migrations (Issue #191)

0.7.7 (2020-02-02)

  • Import Iterable from collections.abc for Python 3.9 compatibility (Issue #189)

  • Add support for Parquet format in UNLOAD command (Issue #187)

0.7.6 (2020-01-17)

  • Fix unhashable type error for sortkey reflection in SQLAlchemy >= 1.3.11 (Issue #180)

  • Expose supported types for import from the dialect (Issue #181)

  • Reflect column comments (Issue #186)

0.7.5 (2019-10-09)

  • Extend psycopg2 package version check to also support psycopg2-binary and psycopg2cffi (Issue #178)

0.7.4 (2019-10-08)

  • Drop hard dependency on psycopg2 but require package to be present on runtime (Issue #165)

  • Switch from info to keyword arguments on columns for SQLAlchemy >= 1.3.0 (Issue #161)

  • Add support for column info on redshift late binding views (Issue #159)

  • Add support for MAXFILESIZE argument to UNLOAD. (Issue #123)

  • Add support for the CREATE LIBRARY command. (Issue #124)

  • Add support for the ALTER TABLE APPEND command. (Issue #162)

  • Add support for the CSV format to UnloadFromSelect. (Issue #169)

  • Update the list of reserved words (adds “az64” and “language”) (Issue #176)

0.7.3 (2019-01-16)

  • Add support for REGION argument to COPY and UNLOAD commands. (Issue #90)

0.7.2 (2018-12-11)

  • Update tests to adapt to changes in Redshift and SQLAlchemy (Issue #140)

  • Add header option to UnloadFromSelect command (Issue #156)

  • Add support for Parquet and ORC file formats in the COPY command (Issue #151)

  • Add official support for Python 3.7 (Issue #153)

  • Avoid manipulating search path in table metadata fetch by using system tables directly (Issue #147)

0.7.1 (2018-01-17)

  • Fix incompatibility of reflection code with SQLAlchemy 1.2.0+ (Issue #138)

0.7.0 (2017-10-03)

  • Do not enumerate search_path with external schemas (Issue #120)

  • Return constraint name from get_pk_constraint and get_foreign_keys

  • Use Enums for Format, Compression and Encoding. Deprecate string parameters for these parameter types (Issue #133)

  • Update included certificate with the transitional ACM cert bundle (Issue #130)

0.6.0 (2017-05-04)

  • Support role-based access control in COPY and UNLOAD commands (Issue #88)

  • Increase max_identifier_length to 127 characters (Issue #96)

  • Fix a bug where table names containing a period caused an error on reflection (Issue #97)

  • Performance improvement for reflection by caching table constraint info (Issue #101)

  • Support BZIP2 compression in COPY command (Issue #110)

  • Allow tests to tolerate new default column encodings in Redshift (Issue #114)

  • Pull in set of reserved words from Redshift docs (Issue #94 <https://github.com/sqlalchemy-redshift/sqlalchemy-redshift/issues/94> _)

0.5.0 (2016-04-21)

  • Support reflecting tables with foriegn keys to tables in non-public schemas (Issue #70)

  • Fix a bug where DISTKEY and SORTKEY could not be used on column names containing spaces or commas. This is a breaking behavioral change for a command like __table_args__ = {‘redshift_sortkey’: (‘foo, bar’)}. Previously, this would sort on the columns named foo and bar. Now, it sorts on the column named foo, bar. (Issue #74)

0.4.0 (2015-11-17)

  • Change the name of the package to sqlalchemy_redshift to match the naming convention for other dialects; the redshift_sqlalchemy package now emits a DeprecationWarning and references sqlalchemy_redshift. The redshift_sqlalchemy compatibility package will be removed in a future release. (Issue #58)

  • Fix a bug where reflected tables could have incorrect column order for some CREATE TABLE statements, particularly for columns with an IDENTITY constraint. (Issue #60)

  • Fix a bug where reflecting a table could raise a NoSuchTableError in cases where its schema is not on the current search_path (Issue #64)

  • Add python 3.5 to the list of versions for integration tests. (Issue #61)

0.3.1 (2015-10-08)

  • Fix breakages to CopyCommand introduced in 0.3.0: Thanks solackerman. (Issue #53)

    • When format is omitted, no FORMAT AS … is appended to the query. This makes the default the same as a normal redshift query.

    • fix STATUPDATE as a COPY parameter

0.3.0 (2015-09-29)

  • Fix view support to be more in line with SQLAlchemy standards. get_view_definition output no longer includes a trailing semicolon and views no longer raise an exception when reflected as Table objects. (Issue #46)

  • Rename RedShiftDDLCompiler to RedshiftDDLCompiler. (Issue #43)

  • Update commands (Issue #52)

    • Expose optional TRUNCATECOLUMNS in CopyCommand.

    • Add all other COPY parameters to CopyCommand.

    • Move commands to their own module.

    • Support inserts into ordered columns in CopyCommand.

0.2.0 (2015-09-04)

  • Use SYSDATE instead of NOW(). Thanks bouk. (Issue #15)

  • Default to SSL with hardcoded AWS Redshift CA. (Issue #20)

  • Refactor of CopyCommand including support for specifying format and compression type. (Issue #21)

  • Explicitly require SQLAlchemy >= 0.9.2 for ‘dialect_options’. (Issue #13)

  • Refactor of UnloadFromSelect including support for specifying all documented redshift options. (Issue #27)

  • Fix unicode issue with SORTKEY on python 2. (Issue #34)

  • Add support for Redshift DELETE statements that refer other tables in the WHERE clause. Thanks haleemur. (Issue #35)

  • Raise NoSuchTableError when trying to reflect a table that doesn’t exist. (Issue #38)

0.1.2 (2015-08-11)

  • Register postgresql.visit_rename_table for redshift’s alembic RenameTable. Thanks bouk. (Issue #7)

0.1.1 (2015-05-20)

  • Register RedshiftImpl as an alembic 3rd party dialect.

0.1.0 (2015-05-11)

  • First version of sqlalchemy-redshift that can be installed from PyPI

Metadata

Release files for chipv-sqlalchemy-redshift 0.8.15.dev0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chipv-sqlalchemy-redshift 0.8.15.dev0
File Size Uploaded
chipv_sqlalchemy_redshift-0.8.15.dev0.tar.gz 52.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chipv-sqlalchemy-redshift 0.8.15.dev0
File Interpreter ABI Platform
chipv_sqlalchemy_redshift-0.8.15.dev0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 91.1 kB

Release files / chipv_sqlalchemy_redshift-0.8.15.dev0.tar.gz

Download URL chipv_sqlalchemy_redshift-0.8.15.dev0.tar.gz
Size 52.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5b2fe098ba899367c9a9b24a1590f2805c6da94c25c21215f6c1c7862972e035
BLAKE2b-256 checksum
How to use checksums
64405ed6f8bae02d285aae32060a64d199f51b94c13ba236782c0566ddc706e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / chipv_sqlalchemy_redshift-0.8.15.dev0-py2.py3-none-any.whl

Download URL chipv_sqlalchemy_redshift-0.8.15.dev0-py2.py3-none-any.whl
Size 39.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
444b1f66332654d7710bd3ea02470e42f42642d436fbfc3e0388597f52494570
BLAKE2b-256 checksum
How to use checksums
c4c767688c20a02f4bd43d2d7f70e8bee57d9178befb793e702061873d8f9e26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.8.15.dev0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page