Convert PostgreSQL to SQLite
6204 단어 데이터 뱅 크
원본 링크:http://manuel.manuelles.nl/blog/2012/01/18/convert-postgresql-to-sqlite/
Today I’d like to share the steps I take when I need to convert aPostgreSQL database into aSQLite database.
Commonly I have to do this when aRuby on Rails application is in production and I have to check some issues with the production data. In the production environment we usually use a PostgreSQL database and for developing
I use a SQLite database, so we need some conversion.
Short story a.k.a I know what I’m doing.
ssh -C [email protected] pg_dump --data-only --inserts YOUR_DB_NAME > dump.sql
SET
SELECT pg_catalog.setval
BEGIN;
as first line and END;
as last line bundle exec rake db:migrate
sqlite3 db/development.sqlite3
sqlite> delete from schema_migrations;
sqlite> .read dump.sql
Longer story a.k.a please explain a little more.
So basically you can do the following 4 major steps to convert the PostgreSQL database into a SQLite database.
1. Generate a SQL dump
First we have to create a sql dump on the production server. I use the following command that results in a
dump.sql
file in the current folder: pg_dump --data-only --inserts YOUR_DB_NAME > dump.sql
I use the
--data-only
option, so it doesn’t generate the schema. Converting the pg_dump generate schema to a valid SQLite schema gave me a lot of difficulties so I chose to generate the schema with the rake db
task (we’ll discuss this in the next step). After you created the dump, you have to download/transfer/mail/etc. that file so you have local access to it.
Trick: Got ssh access?
If you have ssh access, you can also run the following command, which will output the file directly on you local drive
ssh -C [email protected] pg_dump --data-only --inserts YOUR_DB_NAME > dump.sql
2. Modify the dump.sql
There are a few manual find/replace and delete action’s you have to perform on the
dump.sql
file by hand. 2.1 Remove the
SET
statements at the top You will see some statements at the top of the file like
SET statement_timeout = 0;
and SET client_encoding = 'SQL_ASCII';
etc. Remove all of these lines that start with SET
, because SQLite doesn’t use these. 2.2 Remove the setval sequence queries
Under the
SET
queries you’ll see some queries to set the correct sequence for auto incrementing the id’s. SQLite doesn’t keep these value’s in a catalog and must be removed to prevent errors. Remove all the line’s that look like
SELECT pg_catalog.setval('MY_OBJECT_id_seq', 10, true);
2.3 Replace true => ‘t’ and false => ‘f’
The
pg_dump
generate’s true
and false
as value’s for the INSERT INTO
statements. If we want to import these to SQLite we have to replace these to ‘t’ and ‘f’. -- These:
INSERT INTO table_name VALUES (1, true, false);
-- Should be replace to:
INSERT INTO table_name VALUES (1, 't', 'f');
2.4 Transactions. Make it fast!
The first time I imported the dump (that was 2 mb) it took like 12 minutes to complete! After some googling I found out that SQLite’s default behavior is putting each statement into a transaction, which seems to be the time waster (after the fix it toke 12 seconds).
To prevent this behavior you can run the script within 1 transaction by specifying
BEGIN;
at the top of the dump.sql
and END;
at the end of the file. So you would have:
BEGIN;
-- a lot of INSERT INTO statments
END;
3. Recreate the development database
So now we have fetched the production data from the PostgreSQL database, we need to recreate the
development.sqlite3
database. Make a backup and run the migration task
mv db/development.sqlite3 db/development.backup.sqlite3
bundle exec rake db:migrate
Side note when migrating
You must run the migrationuntil the migrated version that is active on the production database. If not, you could have the situation where you have dropped a column and can’t import the dump because the data depends on that column.
Check the
dump.sql
for the latest version number in the schema_migrations
table and migrate to that version. For example for the version
20121701120000
you would do: bundle exec rake db:migrate VERSION=20121701120000
4. Import the dump
The final step is importing the dump file. To do this we have to execute the following command within a terminal:
sqlite3 db/development.sqlite3
sqlite> delete from schema_migrations;
sqlite> .read dump.sql
As you can see we first remove the records from the
schema_migrations
table, because these are also included in the dump.sql
. Of course you could also remove the lines from the file, but I prefer this way. The
.read
command just execute’s all the lines within the specified file. Result
And that’s it! You now have a stuffed
development.sqlite3
database with all the production data out of the PostgreSQL database. byManuel van Rijn Jan 18th, 2012code,postgresql,rails,sqlite
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
nginx websocket ip_해시 규칙프로젝트 를 다운로드 한 후 서로 다른 네트워크 에 각각 이 demo 프로젝트 를 배치 합 니 다. 프로젝트 에서 환경 변수 에 따라 시스템 변 수 를 설정 합 니 다. spring.profiles.active=de...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.