How to convert a postgres database to sqlite

前端 未结 6 699
感动是毒
感动是毒 2020-12-01 16:51

We\'re working on a website, and when we develop locally (one of us from Windows), we use sqlite3, but on the server (linux) we use postgres. We\'d like to be able to impor

相关标签:
6条回答
  • 2020-12-01 17:03

    Taken from https://stackoverflow.com/a/31521432/1680728 (upvote there): The sequel gem makes this a very relaxing procedure:

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

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

    Credits to @lulalala .

    0 讨论(0)
  • 2020-12-01 17:08

    In case one needs a more automatized solution, here's a head start:

    #!/bin/bash
    
    $table_name=TABLENAMEHERE
    
    PGPASSWORD="PASSWORD" /usr/bin/pg_dump --file "results_dump.sql" --host "yourhost.com" --username "username" --no-password --verbose --format=p --create --clean --disable-dollar-quoting --inserts --column-inserts --table "public.${table_name}" "memseq"
    
    # Some clean ups
    perl -0777 -i.original -pe "s/.+?(INSERT)/\1/is" results_dump.sql
    perl -0777 -i.original -pe "s/--.+//is" results_dump.sql
    
    # Remove public. prefix from table name
    sed -i "s/public.${table_name}/${table_name}/g" results_dump.sql
    
    # fix binary blobs
    sed -i "s/'\\\\x/x'/g" results_dump.sql
    
    # use transactions to make it faster
    echo 'BEGIN;' | cat - results_dump.sql > temp && mv temp results_dump.sql
    echo 'END;' >> results_dump.sql
    
    # clean the current table 
    sqlite3 results.sqlite "DELETE FROM ${table_name};"
    
    # finally apply changes
    sqlite3 results.sqlite3 < results_dump.sql && \
    rm results_dump.sql && \
    rm results_dump.sql.original
    
    0 讨论(0)
  • 2020-12-01 17:08

    It was VERY easy for me to do using the taps gem as described here: http://railscasts.com/episodes/342-migrating-to-postgresql

    And I've started using the Postgres.app on my Mac (no install needed, drop the app in your Applications directory, although might have to add one line to your PATH envirnment variable as described in the documentation), with Induction.app as a GUI tool to view/query the database.

    0 讨论(0)
  • 2020-12-01 17:14

    I found this blog entry which guides you to do these steps:

    1. Create a dump of the PostgreSQL database.

      ssh -C username@hostname.com pg_dump --data-only --inserts YOUR_DB_NAME > dump.sql
      
    2. Remove/modify the dump.

      1. Remove the lines starting with SET
      2. Remove the lines starting with SELECT pg_catalog.setval
      3. Replace true for ‘t
      4. Replace false for ‘f
    3. Add BEGIN; as first line and END; as last line

    4. Recreate an empty development database. bundle exec rake db:migrate

    5. Import the dump.

      sqlite3 db/development.sqlite3
      sqlite> delete from schema_migrations;
      sqlite> .read dump.sql
      

    Of course connecting via ssh and creating a new db using rake are optional

    0 讨论(0)
  • 2020-12-01 17:24

    There are some converter tools around:

    https://www2.sqlite.org/cvstrac/wiki?p=ConverterTools

    Would it be easier just to install postgres on windows?

    Probably, and doing so is very straightforward.

    0 讨论(0)
  • 2020-12-01 17:27

    STEP1: make a dump of your database structure and data

    pg_dump --create --inserts -f myPgDump.sql -d myDatabaseName -U myUserName -W myPassword
    

    STEP2: delete everything except CREATE TABLES and INSERT statements out of myPgDump.sql (using text editor)

    STEP3: initialize your SQLite database passing structure and data of your Postgres dump

    sqlite3 myNewSQLiteDB.db -init -myPgDump.sql
    

    STEP4: use your database ;)

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