Fast db insert with postgresql binary copy
.. home-start
.. image:: https://travis-ci.org/altaurog/pgcopy.svg?branch=master :target: https://travis-ci.org/altaurog/pgcopy
.. image:: https://coveralls.io/repos/github/altaurog/pgcopy/badge.svg?branch=master :target: https://coveralls.io/github/altaurog/pgcopy?branch=master
.. image:: https://img.shields.io/pypi/l/pgcopy.svg :target: https://pypi.org/project/pgcopy/
.. image:: https://img.shields.io/pypi/wheel/pgcopy.svg :target: https://pypi.org/project/pgcopy/
.. image:: https://img.shields.io/pypi/pyversions/pgcopy.svg :target: https://pypi.org/project/pgcopy/
Use pgcopy_ for fast data loading into
PostgreSQL with binary copy
_.
.. _binary copy: http://www.postgresql.org/docs/9.3/static/sql-copy.html .. _pgcopy: https://pgcopy.readthedocs.io/en/latest/
.. quickstart-start
::
from datetime import datetime
from pgcopy import CopyManager
import psycopg2
cols = ('id', 'timestamp', 'location', 'temperature')
now = datetime.now()
records = [
(0, now, 'Jerusalem', 72.2),
(1, now, 'New York', 75.6),
(2, now, 'Moscow', 54.3),
]
conn = psycopg2.connect(database='weather_db')
mgr = CopyManager(conn, 'measurements_table', cols)
mgr.copy(records)
# don't forget to commit!
conn.commit()
.. home-end
pgcopy supports the following PostgreSQL scalar types:
Read the docs.
_
.. Read the docs.: pgcopy
cpgcopy_, a Cython implementation, about twice as fast.
.. _binary copy: http://www.postgresql.org/docs/9.3/static/sql-copy.html .. _psycopg2: https://pypi.org/project/psycopg2/ .. _pytz: https://pypi.org/project/pytz/ .. _pytest: https://pypi.org/project/pytest/ .. _cpgcopy: https://github.com/altaurog/cpgcopy .. _Tox: https://tox.readthedocs.io/en/latest/