Skip to content

Instantly share code, notes, and snippets.

@fiftin
Created October 5, 2015 07:04
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save fiftin/18221054c7777e1f1207 to your computer and use it in GitHub Desktop.
Save fiftin/18221054c7777e1f1207 to your computer and use it in GitHub Desktop.
Convert PostgreSQL to SQLite
1. Dump the data only sql to file
$ pg_dump --data-only --inserts YOUR_DB_NAME > dump.sql
2. scp to local
3. Remove the SET statements at the top
such as:
SET statement_timeout = 0;
SET client_encoding = 'SQL_ASCII';
4. Remove the setval sequence queries
such as:
SELECT pg_catalog.setval('MY_OBJECT_id_seq', 10, true);
5. Replace true => ‘t’ and false => ‘f’
-- These:
INSERT INTO table_name VALUES (1, true, false);
-- Should be replace to:
INSERT INTO table_name VALUES (1, 't', 'f');
6. Add BEGIN; and END; to wrap the whole thing as a trasaction
7. Import
$ rm db/development.sqlite3
$ rake db:migrate
$ sqlite3 db/development.sqlite3
> delete from schema_migrations;
> .read dump.sql
> .exit
@makrmark
Copy link

makrmark commented Jan 8, 2024

This was very helpful but may be out of date now.
I found I had to remove "public" prefix from table names, and instead of "t"/"f" I could use true/false values per the export.

Not sure if it was just my sequence but also performed:
delete from ar_internal_metadata;

(along with delete from schema_migrations;)

sqlite> .version
SQLite 3.39.5 2022-10-14 20:58:05 554764a6e721fab307c63a4f98cd958c8428a5d9d8edfde951858d6fd02daapl
clang-14.0.3

@makrmark
Copy link

makrmark commented Jan 8, 2024

Just to wrap this up I had to change some postgresql JSONB fields to JSON, and some Integer/String Array fields to JSON fields.

I also had to use different date calculation queries using sqlite's JULIANDAY and the TRIM functions were different in my search/filter queries and indexes. But that was pretty much it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment