Skip to main content

Arrow -> PostgreSQL encoder

Project description

pgpq

Convert PyArrow RecordBatches to Postgres' native binary format.

Usage

"""Example for README.md"""
from tempfile import mkdtemp
import psycopg
import pyarrow.dataset as ds
import requests
from pgpq import ArrowToPostgresBinaryEncoder

# let's get some example data
tmpdir = mkdtemp()
with open(f"{tmpdir}/yellow_tripdata_2023-01.parquet", mode="wb") as f:
    resp = requests.get(
        "https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2023-01.parquet"
    )
    resp.raise_for_status()
    f.write(resp.content)

# load an arrow dataset
# arrow can load datasets from partitioned parquet files locally or in S3/GCS
# it handles buffering, matching globs, etc.
dataset = ds.dataset(tmpdir)

# create an encoder object which will do the encoding
# and give us the expected Postgres table schema
encoder = ArrowToPostgresBinaryEncoder(dataset.schema)
# get the expected Postgres destination schema
# note that this is _not_ the same as the incoming arrow schema
# and not necessarily the schema of your permanent table
# instead it's the schema of the data that will be sent over the wire
# which for example does not have timezones on any timestamps
pg_schema = encoder.schema()
# assemble ddl for a temporary table
# it's often a good idea to bulk load into a temp table to:
# (1) Avoid indexes
# (2) Stay in-memory as long as possible
# (3) Be more flexible with types
#     (you can't load a SMALLINT into a BIGINT column without casting)
cols = [f'"{col_name}" {col.data_type.ddl()}' for col_name, col in pg_schema.columns]
ddl = f"CREATE TEMP TABLE data ({','.join(cols)})"

with psycopg.connect("postgres://postgres:postgres@localhost:5432/postgres") as conn:
    with conn.cursor() as cursor:
        cursor.execute(ddl)  # type: ignore
        with cursor.copy("COPY data FROM STDIN WITH (FORMAT BINARY)") as copy:
            copy.write(encoder.write_header())
            for batch in dataset.to_batches():
                copy.write(encoder.write_batch(batch))
            copy.write(encoder.finish())
        # load into your actual table, possibly doing type casts
        # cursor.execute("INSERT INTO \"table\" SELECT * FROM data")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgpq-0.9.0.tar.gz (24.9 kB view details)

Uploaded Source

Built Distributions

pgpq-0.9.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (605.6 kB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pgpq-0.9.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (572.8 kB view details)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

pgpq-0.9.0-pp39-pypy39_pp73-macosx_10_7_x86_64.whl (563.5 kB view details)

Uploaded PyPy macOS 10.7+ x86-64

pgpq-0.9.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (605.9 kB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pgpq-0.9.0-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (572.6 kB view details)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

pgpq-0.9.0-pp38-pypy38_pp73-macosx_10_7_x86_64.whl (563.7 kB view details)

Uploaded PyPy macOS 10.7+ x86-64

pgpq-0.9.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (609.2 kB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pgpq-0.9.0-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (575.6 kB view details)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

pgpq-0.9.0-pp37-pypy37_pp73-macosx_10_7_x86_64.whl (565.5 kB view details)

Uploaded PyPy macOS 10.7+ x86-64

pgpq-0.9.0-cp37-abi3-win_amd64.whl (501.0 kB view details)

Uploaded CPython 3.7+ Windows x86-64

pgpq-0.9.0-cp37-abi3-win32.whl (442.2 kB view details)

Uploaded CPython 3.7+ Windows x86

pgpq-0.9.0-cp37-abi3-musllinux_1_2_x86_64.whl (776.2 kB view details)

Uploaded CPython 3.7+ musllinux: musl 1.2+ x86-64

pgpq-0.9.0-cp37-abi3-musllinux_1_2_i686.whl (787.5 kB view details)

Uploaded CPython 3.7+ musllinux: musl 1.2+ i686

pgpq-0.9.0-cp37-abi3-musllinux_1_2_armv7l.whl (806.5 kB view details)

Uploaded CPython 3.7+ musllinux: musl 1.2+ ARMv7l

pgpq-0.9.0-cp37-abi3-musllinux_1_2_aarch64.whl (749.7 kB view details)

Uploaded CPython 3.7+ musllinux: musl 1.2+ ARM64

pgpq-0.9.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (604.6 kB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

pgpq-0.9.0-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (789.9 kB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ s390x

pgpq-0.9.0-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (842.1 kB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ppc64le

pgpq-0.9.0-cp37-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (893.3 kB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ppc64

pgpq-0.9.0-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (543.3 kB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARMv7l

pgpq-0.9.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (572.0 kB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

pgpq-0.9.0-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl (638.7 kB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.5+ i686

pgpq-0.9.0-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.1 MB view details)

Uploaded CPython 3.7+ macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

pgpq-0.9.0-cp37-abi3-macosx_10_7_x86_64.whl (563.7 kB view details)

Uploaded CPython 3.7+ macOS 10.7+ x86-64

File details

Details for the file pgpq-0.9.0.tar.gz.

File metadata

  • Download URL: pgpq-0.9.0.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for pgpq-0.9.0.tar.gz
Algorithm Hash digest
SHA256 7341d9a2c9d905fd782aac4c7c24b98340995a5454f83d36c4918fcafca3d5d8
MD5 a6252a4372c7c6e771c313aa280bd97e
BLAKE2b-256 6d4bfb2642353f18b9cf60e7fe06711f705a1d57d9d108ee88012c95b4ddf586

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 905f820238b970a516cbbd7fe5fed576ba157bd054da8abcae9c3d9e39447605
MD5 5e5140ff363212e43a42743443c97de5
BLAKE2b-256 490bb1480d4d2daf1018489f340b2cc3c9653386eb1173cab963c3af61da4498

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ea1e93168d79a30f99f17f0279e65978e6877b09097b0106503b7b543747aeaf
MD5 b0cd9accaeda462e0a01174a79912777
BLAKE2b-256 885b6b3a353258f09dbca0c466ba9aa0f0cd7d3382e829ce4af593be8e47f49f

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-pp39-pypy39_pp73-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-pp39-pypy39_pp73-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 8c2ad904babf35d966a441a1a6680b6eaef0a47093047c2914d86c8c0b5530a8
MD5 5f7a7771eb511330c041fe50820e5680
BLAKE2b-256 a24abe6cc6f93a218920ad0308ac47422f25e0612048b404627cbc6b8bc2c08b

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 160836cecce86fa60c02ce25166ff20ae03f87957cd0f85ab4aacd64c9891730
MD5 3c51218487f662a28b7779b0f3440aa5
BLAKE2b-256 99a83e3d173e0df878309a2bf67faa1f3c07e026c1a952c16f7086f344203807

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 329ec29c9784f380d0321aac0886363b016bf7da394e5e72bd329fe34ed041e9
MD5 dea4aa9a1a4fe1df18a5ae6acfb9d741
BLAKE2b-256 9583e2821409067d6a71955af2ec01297c7fb4ee10dfbf307663b71b663ae6c7

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-pp38-pypy38_pp73-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-pp38-pypy38_pp73-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 a8f963b79b54e10d6ce015b37a6ede386e9db450780623453cdcef18407d646c
MD5 fd96e85f93d603dcca7e7ae6bdb97097
BLAKE2b-256 6ee82e8dc2b4128619d5b764a673249b77bcbedf22dc9a9d895770850b9cb48b

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 98aa74978b0b85076aaec6022bc0cef68417ac8922d8e53e7c8deabe70f35bab
MD5 9a48d4160e28a9efc4fb7641ae2d5fe5
BLAKE2b-256 a7b8f492eb30c71d0bbf4e63396eeaceadc60a78b366413568c28bd4ecd6b3d0

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 259b0aca8b06a1757dc16e668457a3a30a8074cd095612c2312c2e400e7f5418
MD5 cdbec72a1182a888a8307b88148be990
BLAKE2b-256 c5785535b0c119b987d90e57192e2549b6923868416a6c388b98aa4edffc6096

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-pp37-pypy37_pp73-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-pp37-pypy37_pp73-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 c6463e1b28ffc33efcb37d7fc5d4bf144078fc2f4bd8fc74c114e9883ae66b76
MD5 f0f13b66ccfa25ee7a41dd95bacee150
BLAKE2b-256 0a1598a0775d95bac085746d25fb320b78ffdca8abf029da2e5f5067dca5395f

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-win_amd64.whl.

File metadata

  • Download URL: pgpq-0.9.0-cp37-abi3-win_amd64.whl
  • Upload date:
  • Size: 501.0 kB
  • Tags: CPython 3.7+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 1058f1ebb6cf2e879351c42af4cc7836bcd239cdff9690ba5c738b74450e9cc7
MD5 1844874c39ce1c8469539a78f2cc88d8
BLAKE2b-256 df96e8f76dfcf12cbb40bda62867ec96aea19af626439578a0a602b309085973

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-win32.whl.

File metadata

  • Download URL: pgpq-0.9.0-cp37-abi3-win32.whl
  • Upload date:
  • Size: 442.2 kB
  • Tags: CPython 3.7+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-win32.whl
Algorithm Hash digest
SHA256 56d06b6c2acdb0331135b298ffc6c9ea9c2785e153256c5cc1d2adbc0c85d096
MD5 315a104fa250822198018d9faa559da9
BLAKE2b-256 3819896bec40ba040141cfb4bf028c940152ccd6ca80bfc756a70ea6bdcad041

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 acd844a36807db1ecb18289f24001d44a3fc8aefff01fe6f7a4b352fda659960
MD5 11e933a4f7789d8bad10d60c035e7aa9
BLAKE2b-256 f81ec86c3c8807d48eccddc555e74c3c130d2baa07d1ff0626d87c8d7e2932bc

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-musllinux_1_2_i686.whl.

File metadata

  • Download URL: pgpq-0.9.0-cp37-abi3-musllinux_1_2_i686.whl
  • Upload date:
  • Size: 787.5 kB
  • Tags: CPython 3.7+, musllinux: musl 1.2+ i686
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 3e7cd18d0fa7fc216216561686cfa1da1c0ce6f2e175771b6f291edcdc0fb7b5
MD5 d6abdbbe361bb22fe8d89b9d7b2eb131
BLAKE2b-256 7c647703af6d71c9e5be32015f6a9ecffc797b09bf7ce071a2f0c62c1b3ecdff

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 07068d5bf10fc54fafa6e28ed2c7551422d3366cce931f706ec184ed19ebac73
MD5 c5ed8ce413bce8df5555d3109c97e7e0
BLAKE2b-256 07680db33bc0540789ba6ec2b8eda176a7fbd309f6dfc26bd056981de46e77c9

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 5fac7b1c0f64e0bf7f0c11e81720b527593acc2b83c48d20669fd059e5e67f24
MD5 42c021d8e7fd76083345c080a7b36470
BLAKE2b-256 7cfe10edc9c81f5fbc7cceffb1d5415bd7ef441021791192956f0ffd59254d79

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9a34708f8a1d5ad202c5e7726969638f0dadffd8a9463ef23c00db51e3a110ec
MD5 1f74ff90478199af1a4d5d447114cb55
BLAKE2b-256 5e1e0561c2fa8ca9f43781aba5cd3badb379bd3c59d39538cee9ef1461cf8fbd

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 a15a348d695e0a4a4998ab1ef0b8bbf5b927cbf53a124a89f9b7c3c51967833d
MD5 971822f5388fb89ade70189471e8a321
BLAKE2b-256 b4083d6f0c5086d8059ff9ac53785a34dbf6bb3f4cac4ffab8ec7cd5aa23141e

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 ea460f7abdd1bb17fdc8b2312f63f141d5e93c21a28f93dd47309db4a5cdfa01
MD5 69bfa135ee973c837ee1ab0bb65dae3c
BLAKE2b-256 6c1449decb622edf24b0b5a3a1841748ba455caed9e024a3fa3fdc6ad1320427

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl
Algorithm Hash digest
SHA256 d39a369f2b77db2428850d85622ed5fa0506fc89a58ac9122cb83c1535273231
MD5 682e45caef0bf5e30765f8fc2e8d8126
BLAKE2b-256 b09f7812366dd4e04d003e6209bf70a81e884d9685bb2840e66056ccb7f72bdc

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 cd00b7bb24e58d7dc14e7a1f51f15c5f6f69ad64502f8ee68b6963c214d10a86
MD5 78a129c810ae6bfce966f3c677adad3a
BLAKE2b-256 27fd3359b4079439cc8d0e8746fadede0198de09a8f0c899c804f01ebcad8a19

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ea47fcd7c5fae68f77a12a3a1b32d68f2f28a0dbccb98d89366d6e5973bb5c26
MD5 4fde2e9b4b5d9a4da66b7598f4b206bc
BLAKE2b-256 e78f8dc0ddfafa31a35d0c32429b8d43583a79ac9369366c6b52a7ef6477f1a5

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 105b9bca22409fbb8adc8f2c1fc29d887c8d3bfdb8ab0defe911226f1681d3b1
MD5 b3823bc80ac64827b8aea92356e3e7a9
BLAKE2b-256 2b1d9d465ea07f59fe3c256af3a0f40713b59646bed0a8366b26cf63a9888f9f

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 b1c11911f7481b82c1a5272792953e40efe1d7e49290d4da787f5a927a1f3b1b
MD5 6a709f02eaff9c5a191a50b4b59c39ad
BLAKE2b-256 5f146bdd50f6bef237f398a6b3f6c335568c596c39b5c62d0a1a6e44b0441c1e

See more details on using hashes here.

File details

Details for the file pgpq-0.9.0-cp37-abi3-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for pgpq-0.9.0-cp37-abi3-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 5d16e6f8c86339ae376e9f62ec5a15e73a1af4e39f982bf456904c03395eb2fa
MD5 28e298dffeb74431b21301f13aaf9db4
BLAKE2b-256 ca5b3ec6ced557b6bde764c8bf527c4fef10ba4fe8ae50c399b74feb30bcc40d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page