database 如何将 postgres 数据库转换为 sqlite
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6148421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a postgres database to sqlite
提问by luqui
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.
我们正在开发一个网站,当我们在本地开发时(其中一个来自 Windows),我们使用 sqlite3,但在服务器(linux)上我们使用 postgres。我们希望能够将生产数据库导入到我们的开发过程中,所以我想知道是否有办法将 postgres 数据库转储转换为 sqlite3 可以理解的东西(只需将 postgres 的转储 SQL 提供给它,许多错误)。或者只是在 Windows 上安装 postgres 会更容易吗?谢谢。
采纳答案by Denis de Bernardy
There are some converter tools around:
周围有一些转换器工具:
http://sqlite.com/cvstrac/wiki?p=ConverterTools
http://sqlite.com/cvstrac/wiki?p=ConverterTools
Would it be easier just to install postgres on windows?
在 Windows 上安装 postgres 会更容易吗?
Probably, and doing so is very straightforward.
可能,这样做非常简单。
回答by tutuDajuju
I found this blog entrywhich guides you to do these steps:
我找到了这个博客条目,它指导您执行以下步骤:
Create a dump of the PostgreSQL database.
ssh -C [email protected] pg_dump --data-only --inserts YOUR_DB_NAME > dump.sqlRemove/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:migrateImport the dump.
sqlite3 db/development.sqlite3 sqlite> delete from schema_migrations; sqlite> .read dump.sql
创建 PostgreSQL 数据库的转储。
ssh -C [email protected] pg_dump --data-only --inserts YOUR_DB_NAME > dump.sql删除/修改转储。
- 删除以开头的行
SET - 删除以开头的行
SELECT pg_catalog.setval - 将“
t”替换为 true - 将 false 替换为“
f”
- 删除以开头的行
添加
BEGIN;为第一行和END;最后一行重新创建一个空的开发数据库。
bundle exec rake db:migrate导入转储。
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
当然,通过 ssh 连接并使用 rake 创建新数据库是可选的
回答by Artem Zaytsev
STEP1: make a dump of your database structure and data
STEP1:转储您的数据库结构和数据
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)
STEP2:从 myPgDump.sql 中删除除 CREATE TABLES 和 INSERT 语句之外的所有内容(使用文本编辑器)
STEP3: initialize your SQLite database passing structure and data of your Postgres dump
STEP3:初始化您的 SQLite 数据库,传递 Postgres 转储的结构和数据
sqlite3 myNewSQLiteDB.db -init -myPgDump.sql
STEP4: use your database ;)
第 4 步:使用您的数据库 ;)
回答by random_user
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)//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
回答by jpw
It was VERY easy for me to do using the taps gem as described here: http://railscasts.com/episodes/342-migrating-to-postgresql
使用这里描述的 taps gem 对我来说非常容易: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.
而且我已经开始在我的 Mac 上使用 Postgres.app(不需要安装,将应用程序放在您的应用程序目录中,尽管可能需要在您的 PATH 环境变量中添加一行,如文档中所述),将 Induction.app 作为用于查看/查询数据库的 GUI 工具。

