28

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.

Shadow
  • 33,525
  • 10
  • 51
  • 64
luqui
  • 59,485
  • 12
  • 145
  • 204
  • 22
    I'd just switch your development environment to PostgreSQL, developing on top of one database (especially one as loose and forgiving as SQLite) but deploying on another (especially one as strict as PostgreSQL) is generally a recipe for aggravation and swearing. – mu is too short May 27 '11 at 07:15
  • 12
    To echo mu's response, DON'T DO THIS..DON'T DO THIS..DON'T DO THIS. Develop and deploy on the same thing. It's bad engineering practice to do otherwise. – Kuberchaun May 27 '11 at 14:38
  • 2
    Developing on SQLite and running on postgres is perfectly acceptable IF you are using an ORM which deals with all the differences in dialect and schema for you. – jwg Feb 23 '18 at 09:11
  • 1
    @Zaz You meant https://serverfault.com/questions/274355/how-to-convert-a-postgres-database-to-sqlite – Andrew Grimm Dec 10 '18 at 04:02
  • [The same question on ServerFault.](https://serverfault.com/q/274355/49785) – Zaz Dec 11 '18 at 20:59
  • https://phauer.com/2017/dont-use-in-memory-databases-tests-h2/ –  Mar 29 '22 at 12:07

8 Answers8

26

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

tutuDajuju
  • 10,307
  • 6
  • 65
  • 88
  • 4
    For me, `t` and `f` did not work, I had to use `1` and `0` as described in these posts: http://stackoverflow.com/questions/5768364/activerecordstatementinvalid-sqlite3sqlexception-no-such-column-true – neRok Jun 11 '16 at 03:35
  • 1
    blobs saved in different format. sqlite uses x'010101', while postgres '\x0101' – Evgen Bodunov Oct 18 '16 at 11:57
  • 1
    I created a gist that was based from this and other sources: https://gist.github.com/jeffreycastro/918a24588ac1628e2af6b0def5bb7416 – Jeffrey M Castro Nov 29 '19 at 03:40
  • answer is not complete. check here - https://medium.com/@andreypu/how-to-port-postgresql-db-to-sqlite-db-e991b0d82287 more detailed explanation. helped me out – Kirguduck Dec 15 '21 at 15:26
13

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 ;)

dfrankow
  • 20,191
  • 41
  • 152
  • 214
Artem Zaytsev
  • 1,621
  • 20
  • 19
5

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 .

Felix
  • 4,510
  • 2
  • 31
  • 46
3

You can use pg2sqlite for converting pg_dump output to sqlite.

#  Making dump
pg_dump -h host -U user -f database.dump database

#  Making sqlite database
pg2sqlite -d database.dump -o sqlite.db

Schemas is not supported by pg2sqlite, and if you dump contains schema then you need to remove it. You can use this script:

# sed 's/<schema name>\.//' -i  database.dump
sed 's/public\.//' -i  database.dump
pg2sqlite -d database.dump -o sqlite.db
caiiiycuk
  • 1,466
  • 14
  • 20
2

Even though there are many very good helpful answers here, I just want to mark this as answered. We ended up going with the advice of the comments:

I'd just switch your development environment to PostgreSQL, developing on top of one database (especially one as loose and forgiving as SQLite) but deploying on another (especially one as strict as PostgreSQL) is generally a recipe for aggravation and swearing. – @mu is too short

To echo mu's response, DON'T DO THIS..DON'T DO THIS..DON'T DO THIS. Develop and deploy on the same thing. It's bad engineering practice to do otherwise. – @Kuberchaun

So we just installed postgres on our dev machines. It was easy to get going and worked very smoothly.

luqui
  • 59,485
  • 12
  • 145
  • 204
1

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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
random_user
  • 820
  • 1
  • 7
  • 18
0

when I faced with same issue I did not find any useful advices on Internet. My source PostgreSQL db had very complicated schema.
You just need to remove from your db-file manually everything besides table creating
More details - here

Kirguduck
  • 748
  • 1
  • 9
  • 20
-1

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.

jpw
  • 18,697
  • 25
  • 111
  • 187
  • But this question is about going the other way around - from postgres to sqlite, not to postgres, as your comment and reference suggest. – nealmcb Dec 31 '12 at 18:09