如何将 postgres 数据库转换为 SQLite?

发布于 2024-11-10 14:34:43 字数 224 浏览 8 评论 0原文

我们正在开发一个网站,当我们在本地开发时(其中一个来自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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

往日情怀 2024-11-17 14:34:43

2024 更新 - 通过更新 SQL 转储来执行此操作可能非常复杂,也可能非常简单,具体取决于您使用的 Postgres 特定功能(例如 hstore 列)。

对于一个简单的用例,我发现 此博客条目,它将指导您执行以下步骤:

  1. 创建 PostgreSQL 的转储数据库。

     ssh -C [电子邮件受保护] pg_dump --仅数据--插入YOUR_DB_NAME >转储.sql
    
  2. 删除/修改转储。

    1. 删除以 SET 开头的行
    2. 删除以 SELECT pg_catalog.setval 开头的行
    3. 将“t”替换为 true
    4. 将“f”替换为 false
  3. 添加 BEGIN; 作为第一行,添加 END; 作为最后一行

  4. 重新创建一个空的开发数据库。 捆绑执行 rake db:migrate

  5. 导入转储。

    <前><代码> 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:

  1. Create a dump of the PostgreSQL database.

     ssh -C [email protected] pg_dump --data-only --inserts YOUR_DB_NAME > dump.sql
    
  2. Remove/modify the dump.

    1. Remove the lines starting with SET
    2. Remove the lines starting with SELECT pg_catalog.setval
    3. Replace true for ‘t
    4. Replace false for ‘f
  3. Add BEGIN; as first line and END; as last line

  4. Recreate an empty development database. bundle exec rake db:migrate

  5. 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.

If you'd like to expand on this, please feel free to add more details here and I'll edit the answer.

夏九 2024-11-17 14:34:43

第 1 步:转储数据库结构和数据

pg_dump --create --inserts -f myPgDump.sql \
   -d myDatabaseName -U myUserName -W myPassword

第 2 步:从 myPgDump.sql 中删除除 CREATE TABLES 和 INSERT 语句之外的所有内容(使用文本编辑器)

第 3 步:初始化 SQLite 数据库,传递 Postgres 转储的结构和数据

sqlite3 myNewSQLiteDB.db -init myPgDump.sql

第 4 步:使用数据库;)

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 ;)

失退 2024-11-17 14:34:43

您可以使用 pg2sqlite 将 pg_dump 输出转换为 sqlite。

#  Making dump
pg_dump -h host -U user -f database.dump database

#  Making sqlite database
pg2sqlite -d database.dump -o sqlite.db

pg2sqlite 不支持模式,如果转储包含模式,则需要将其删除。您可以使用这个脚本:

# sed 's/<schema name>\.//' -i  database.dump
sed 's/public\.//' -i  database.dump
pg2sqlite -d database.dump -o sqlite.db

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
美胚控场 2024-11-17 14:34:43

尽管这里有很多非常有用的答案,但我只想将其标记为已回答。我们最终采纳了评论的建议:

我只是将你的开发环境切换到 PostgreSQL,在一个数据库(尤其是像 SQLite 这样宽松和宽容的数据库)上进行开发,但在另一个数据库(尤其是像 PostgreSQL 这样严格的数据库)上部署通常会导致恼怒和咒骂。 。 –
@mu 太短了

为了回应 mu 的回应,不要这样做..不要这样做..不要这样做。开发和部署在同一件事上。否则的话,这是不好的工程实践。 – @Kuberchaun

所以我们刚刚在我们的开发机器上安装了 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:

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.

々眼睛长脚气 2024-11-17 14:34:43

如果需要更自动化的解决方案,这里是一个好的开始:

#!/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

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
2024-11-17 14:34:43

当我遇到同样的问题时,我在互联网上没有找到任何有用的建议。我的源 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

为你拒绝所有暧昧 2024-11-17 14:34:43

使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文