Skip to content

Instantly share code, notes, and snippets.

@josemarcosrf
Created July 13, 2022 21:38
Show Gist options
  • Save josemarcosrf/9bd66b15ad39454d28281b08cf2198f7 to your computer and use it in GitHub Desktop.
Save josemarcosrf/9bd66b15ad39454d28281b08cf2198f7 to your computer and use it in GitHub Desktop.
SQLite to Postgress migration

SQLite to Postgres

Extracting from SQLite

We have two options:

  1. Create a dump:
sqlite3 database.sqlite .dump > /path/to/sqlite-dumpfile.sql
  1. Or we can export all tables as CSV files
for table in $(sqlite3 database.sqlite ".tables"); do
	echo $table
	sqlite3 -header -csv \
		database.sqlite "select * from $table;" > "$table.csv"
done

Importing to Postgres

Using CSV export files

  1. Connect to Postgres using psql
psql <postgres-connection-string>
  1. Change to the databse where you want to import
\c <your-database>
  1. Import each CSV file
\copy "<table-name>" from "<your/exports/table-name.csv>" delimiter ',' csv header;

Using a dump file

TBD

Using pgloader

This might find issues depending ont he structure of the tables and its data types

  1. Create a pgload.conf file with:

    load database  
        from 'database.sqlite'  
        into postgresql:///testdb 
           
    with include drop, create tables, create indexes, reset sequences  
             
    set work_mem to '16MB', maintenance_work_mem to '512 MB';
    
  2. Run: pgloader pgload.conf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment