如何将 postgres 数据库转换为 SQLite?
我们正在开发一个网站,当我们在本地开发时(其中一个来自Windows),我们使用sqlite3,但在服务器(linux)上我们使用postgres。我们希望能够将生产数据库导入到我们的开发过程中,所以我想知道是否有一种方法可以将 postgres 数据库转储转换为 sqlite3 可以理解的内容(只需将 postgres 转储的 SQL 提供给它即可,许多错误)。或者在 Windows 上安装 postgres 会更容易吗?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
2024 更新 - 通过更新 SQL 转储来执行此操作可能非常复杂,也可能非常简单,具体取决于您使用的 Postgres 特定功能(例如 hstore 列)。
对于一个简单的用例,我发现 此博客条目,它将指导您执行以下步骤:
创建 PostgreSQL 的转储数据库。
删除/修改转储。
SET
开头的行SELECT pg_catalog.setval
开头的行t
”替换为 truef
”替换为 false添加
BEGIN;
作为第一行,添加END;
作为最后一行重新创建一个空的开发数据库。
捆绑执行 rake db:migrate
导入转储。
<前><代码> sqlite3 db/development.sqlite3
sqlite>从 schema_migrations 中删除;
sqlite> .读取转储.sql
当然,通过 ssh 连接并使用 rake 创建新数据库是可选的。
如果您想对此进行扩展,请随时在此处添加更多详细信息,我将编辑答案。
2024 update - doing this by updating a SQL dump can be very complicated, or very simple, depending on what Postgres-specific features (e.g hstore columns) you use.
For a simple use case, I found this blog entry which guides you to do these steps:
Create a dump of the PostgreSQL database.
Remove/modify the dump.
SET
SELECT pg_catalog.setval
t
’f
’Add
BEGIN;
as first line andEND;
as last lineRecreate an empty development database.
bundle exec rake db:migrate
Import the dump.
Of course connecting via ssh and creating a new db using rake are optional.
If you'd like to expand on this, please feel free to add more details here and I'll edit the answer.
第 1 步:转储数据库结构和数据
第 2 步:从 myPgDump.sql 中删除除 CREATE TABLES 和 INSERT 语句之外的所有内容(使用文本编辑器)
第 3 步:初始化 SQLite 数据库,传递 Postgres 转储的结构和数据
第 4 步:使用数据库;)
STEP1: make a dump of your database structure and data
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
STEP4: use your database ;)
您可以使用 pg2sqlite 将 pg_dump 输出转换为 sqlite。
pg2sqlite 不支持模式,如果转储包含模式,则需要将其删除。您可以使用这个脚本:
You can use pg2sqlite for converting pg_dump output to sqlite.
Schemas is not supported by pg2sqlite, and if you dump contains schema then you need to remove it. You can use this script:
尽管这里有很多非常有用的答案,但我只想将其标记为已回答。我们最终采纳了评论的建议:
所以我们刚刚在我们的开发机器上安装了 postgres。上手很容易,工作也很顺利。
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:
So we just installed postgres on our dev machines. It was easy to get going and worked very smoothly.
如果需要更自动化的解决方案,这里是一个好的开始:
In case one needs a more automatized solution, here's a head start:
当我遇到同样的问题时,我在互联网上没有找到任何有用的建议。我的源 PostgreSQL 数据库有非常复杂的架构。
您只需从数据库文件中手动删除除创建表之外的所有内容
更多详细信息 - 此处
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
使用 Taps gem 对我来说非常容易,如下所述:
http://railscasts.com/episodes/342-migration-to-postgresql
我已经开始在我的 Mac 上使用 Postgres.app(无需安装,将应用程序放在应用程序目录中,尽管可能需要按照文档中的说明向 PATH 环境变量添加一行),并将 Induction.app 作为用于查看/查询数据库的 GUI 工具。
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.