Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

postgresql - How to convert a postgres database to sqlite

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 import the production database into our development process, so I'm wondering if there is a way to convert from a postgres database dump to something sqlite3 can understand (just feeding it the postgres's dumped SQL gave many, many errors). Or would it be easier just to install postgres on windows? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...