Convert SQLITE SQL dump file to POSTGRESQL

后端 未结 8 540
梦如初夏
梦如初夏 2020-11-28 01:03

I\'ve been doing development using SQLITE database with production in POSTGRESQL. I just updated my local database with a huge amount of data and need to transfer a specifi

相关标签:
8条回答
  • 2020-11-28 01:16

    I wrote a script to do the sqlite3 to postgres migration. It doesn't handle all the schema/data translations mentioned in https://stackoverflow.com/a/4581921/1303625, but it does what I needed it to do. Hopefully it will be a good starting point for others.

    https://gist.github.com/2253099

    0 讨论(0)
  • 2020-11-28 01:18

    I have tried editing/regexping the sqlite dump so PostgreSQL accepts it, it is tedious and prone to error.

    What I got to work really fast:

    First recreate the schema on PostgreSQL without any data, either editing the dump or if you were using an ORM you may be lucky and it talks to both back-ends (sqlalchemy, peewee, ...).

    Then migrate the data using pandas. Suppose you have a table with a bool field (which is 0/1 in sqlite, but must be t/f in PostgreSQL)

    def int_to_strbool(df, column):
        df = df.replace({column: 0}, 'f')
        df = df.replace({column: 1}, 't')
        return df
    
    #def other_transform(df, column):
    #...
    
    conn = sqlite3.connect(db)
    df = pd.read_sql(f'select * from {table_name}', conn)
    
    df = int_to_strbool(df, bool_column_name)
    #df = other_transform(df, other_column_name)
    
    df.to_csv(table_name + '.csv'), sep=',', header=False, index=False)
    

    This works like a charm, is easy to write, read and debug each function, unlike (for me) the regular expressions.

    Now you can try to load the resulting csv with PostgreSQL (even graphically with the admin tool), with the only caveat that you must load the tables with foreign keys after you have loaded the tables with the corresponding source keys. I did not have the case of a circular dependency, I guess you can suspend temporarily the key checking if that is the case.

    0 讨论(0)
  • 2020-11-28 01:27

    The sequel gem (a Ruby library) offers data copying across different databases: http://sequel.jeremyevans.net/rdoc/files/doc/bin_sequel_rdoc.html#label-Copy+Databases

    First install Ruby, then install the gem by running gem install sequel.

    In case of sqlite, it would be like this: sequel -C sqlite://db/production.sqlite3 postgres://user@localhost/db

    0 讨论(0)
  • 2020-11-28 01:29

    pgloader work wonders on converting database in sqlite to postgresql.

    Here's an example on converting a local sqlitedb to a remote PostgreSQL db:

    pgloader sqlite.db postgresql://username:password@hostname/dbname

    0 讨论(0)
  • 2020-11-28 01:30

    Try these steps...

    Step 01: Dump sqlite db to json

    python3 manage.py dumpdata > data.json
    

    Step 02: Create tables without migration

    python3 manage.py migrate --run-syncdb
    

    Step 03: Open django shell. Then exclude contentype data

    python3 manage.py shell
    from django.contrib.contenttypes.models import ContentType
    ContentType.objects.all().delete()
    quit()
    

    Step 04: Load Data

    python3 manage.py loaddata data.json
    
    0 讨论(0)
  • 2020-11-28 01:31

    You should be able to feed that dump file straight into psql:

    /path/to/psql -d database -U username -W < /the/path/to/sqlite-dumpfile.sql
    

    If you want the id column to "auto increment" then change its type from "int" to "serial" in the table creation line. PostgreSQL will then attach a sequence to that column so that INSERTs with NULL ids will be automatically assigned the next available value. PostgreSQL will also not recognize AUTOINCREMENT commands, so these need to be removed.

    You'll also want to check for datetime columns in the SQLite schema and change them to timestamp for PostgreSQL. (Thanks to Clay for pointing this out.)

    If you have booleans in your SQLite then you could convert 1 and 0 to 1::boolean and 0::boolean (respectively) or you could change the boolean column to an integer in the schema section of the dump and then fix them up by hand inside PostgreSQL after the import.

    If you have BLOBs in your SQLite then you'll want to adjust the schema to use bytea. You'll probably need to mix in some decode calls as well. Writing a quick'n'dirty copier in your favorite language might be easier than mangling the SQL if you a lot of BLOBs to deal with though.

    As usual, if you have foreign keys then you'll probably want to look into set constraints all deferred to avoid insert ordering problems, placing the command inside the BEGIN/COMMIT pair.

    Thanks to Nicolas Riley for the boolean, blob, and constraints notes.

    If you have ` on your code, as generated by some SQLite3 clients, you need to remove them.

    PostGRESQL also doesn't recognize unsigned columns, so you might want to drop that or add a custom-made constraint such as this:

    CREATE TABLE tablename (
        ...
        unsigned_column_name integer CHECK (unsigned_column_name > 0)
    );
    

    While SQLite defaults null values to '', PostgreSQL requires them to be set as NULL.

    The syntax in the SQLite dump file appears to be mostly compatible with PostgreSQL so you can patch a few things and feed it to psql. Importing a big pile of data through SQL INSERTs might take a while but it'll work.

    0 讨论(0)
提交回复
热议问题