Convert SQLITE SQL dump file to POSTGRESQL

后端 未结 8 541
梦如初夏
梦如初夏 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:31

    pgloader

    I came across this post when searching for a way to convert an SQLite dump to PostgreSQL. Even though this post has an accepted answer (and a good one at that +1), I think adding this is important.

    I started looking into the solutions here and realized that I was looking for a more automated method. I looked up the wiki docs:

    https://wiki.postgresql.org/wiki/Converting_from_other_Databases_to_PostgreSQL

    and discovered pgloader. Pretty cool application and it's relatively easy to use. You can convert the flat SQLite file into a usable PostgreSQL database. I installed from the *.deb and created a command file like this in a test directory:

    load database  
        from 'db.sqlite3'  
        into postgresql:///testdb 
           
    with include drop, create tables, create indexes, reset sequences  
             
    set work_mem to '16MB', maintenance_work_mem to '512 MB';
    

    like the docs state. I then created a testdb with createdb:

    createdb testdb

    I ran the pgloader command like this:

    pgloader command

    and then connected to the new database:

    psql testdb

    After some queries to check the data, it appears it worked quite well. I know if I had tried to run one of these scripts or do the stepwise conversion mentioned herein, I would have spent much more time.

    To prove the concept I dumped this testdb and imported into a development environment on a production server and the data transferred over nicely.

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

    You can use a one liner, here is an example with the help of sed command:

    sqlite3 mjsqlite.db .dump | sed -e 's/INTEGER PRIMARY KEY AUTOINCREMENT/SERIAL PRIMARY KEY/' | sed -e 's/PRAGMA foreign_keys=OFF;//' | sed -e 's/unsigned big int/BIGINT/g' | sed -e 's/UNSIGNED BIG INT/BIGINT/g' | sed -e 's/BIG INT/BIGINT/g' | sed -e 's/UNSIGNED INT(10)/BIGINT/' | sed -e 's/BOOLEAN/SMALLINT/g' | sed -e 's/boolean/SMALLINT/g' | sed -e 's/UNSIGNED BIG INT/INTEGER/g' | sed -e 's/INT(3)/INT2/g' | sed -e 's/DATETIME/TIMESTAMP/g' | psql mypqdb mypguser 
    
    0 讨论(0)
提交回复
热议问题