pysqlsync

raw JSON →
0.8.4 verified Fri May 01 auth: no python

A Python library for synchronizing database schema and large volumes of data between different database engines. Version 0.8.4 supports PostgreSQL, MySQL, SQLite, and MSSQL. Active development with monthly releases.

pip install pysqlsync
error ModuleNotFoundError: No module named 'pysqlsync.engine'
cause Imports changed in v0.8.0; engines moved under pysqlsync.sync.
fix
Use 'from pysqlsync.sync.postgresql import PostgreSQLSyncEngine'.
error AttributeError: 'PostgreSQLSyncEngine' object has no attribute 'sync_data'
cause Older API used 'sync'; renamed to 'sync_data' in v0.7.0.
fix
Replace engine.sync(...) with await engine.sync_data(...).
error RuntimeError: asyncio.run() cannot be called from a running event loop
cause pysqlsync functions are async and require a running loop, but calling asyncio.run() inside a nested context is invalid.
fix
Ensure only one call to asyncio.run() or use await inside existing async context.
error ModuleNotFoundError: No module named 'asyncpg'
cause PostgreSQL driver not installed.
fix
pip install pysqlsync[postgresql] or pip install asyncpg
breaking Version 0.8.0 renamed 'SQLSyncEngine' to 'SyncEngine' and moved engines to submodules like 'pysqlsync.sync.postgresql'. Old imports from 'pysqlsync.engine.*' will fail.
fix Update imports to use 'from pysqlsync.sync.postgresql import PostgreSQLSyncEngine' instead of 'from pysqlsync.engine.postgresql import PostgreSQLSyncEngine'.
deprecated The 'mode' parameter in sync_data() with value 'replace' is deprecated in favor of 'upsert' or 'overwrite' since 0.7.0.
fix Use mode='upsert' (insert or update) or mode='overwrite' (delete and insert).
gotcha All operations require an active event loop; running without asyncio.get_event_loop() will fail. Engine.connect() is async and must be awaited.
fix Always run sync code inside async functions and use asyncio.run().
gotcha If optional database driver is not installed, import raises ModuleNotFoundError. pysqlsync does not auto-install drivers.
fix Install required extras: pip install pysqlsync[postgresql] for PostgreSQL, etc.
pip install pysqlsync[postgresql,mysql]

Connect to PostgreSQL, define a table, and sync data with upsert mode.

import asyncio
from pysqlsync.sync.postgresql import PostgreSQLSyncEngine
from pysqlsync.model import TableDef

async def main():
    # Connect and sync
    engine = PostgreSQLSyncEngine(
        host='localhost',
        port=5432,
        user='user',
        password=os.environ.get('PGPASSWORD', ''),
        database='mydb'
    )
    await engine.connect()
    
    # Define a table
    table = TableDef(name='users', columns=[
        {'name': 'id', 'type': 'integer', 'primary_key': True},
        {'name': 'name', 'type': 'text'}
    ])
    
    # Create table and sync data
    await engine.create_table(table)
    await engine.sync_data(table, [{'id': 1, 'name': 'Alice'}], mode='upsert')
    
    await engine.close()

asyncio.run(main())