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.
-
22I'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
-
12To 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
-
2Developing 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 Answers
I found this blog entry which guides you to do these steps:
Create a dump of the PostgreSQL database.
ssh -C username@hostname.com pg_dump --data-only --inserts YOUR_DB_NAME > dump.sql
Remove/modify the dump.
- Remove the lines starting with
SET
- Remove the lines starting with
SELECT pg_catalog.setval
- Replace true for ‘
t
’ - Replace false for ‘
f
’
- Remove the lines starting with
Add
BEGIN;
as first line andEND;
as last lineRecreate an empty development database.
bundle exec rake db:migrate
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

- 10,307
- 6
- 65
- 88
-
4For 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
-
1blobs saved in different format. sqlite uses x'010101', while postgres '\x0101' – Evgen Bodunov Oct 18 '16 at 11:57
-
1I 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
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 ;)

- 20,191
- 41
- 152
- 214

- 1,621
- 20
- 19
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 .

- 4,510
- 2
- 31
- 46
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

- 1,466
- 14
- 20
-
1Tried this tool. It ran for a few seconds, reported no errors and produced a sqlite db of zero bytes :/ – boxed May 11 '22 at 07:08
-
This programme currently fails with exceptions against a dump from Postgresql 15. – Luís de Sousa Jan 19 '23 at 09:13
-
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.

- 59,485
- 12
- 145
- 204
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

- 732,580
- 175
- 1,330
- 1,459

- 820
- 1
- 7
- 18
-
great script ! what about `boolean` values ? what do you do with those ? – Ricky Levi Jul 01 '19 at 18:57
-
What's `memseq`? I'm getting this error: `pg_dump: error: too many command-line arguments (first is "memseq")` – bmaupin Oct 07 '21 at 19: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.

- 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