首页 > 解决方案 > 将 SQLite 转储迁移到部署在 Kubernetes 上的 PostgreSQL

问题描述

我正在尝试将 SQLite 转储迁移到部署在 Kubernetes 上的 PostgreSQL,但在连接点时遇到问题。

我使用以下命令创建了一个 SQLite 转储:

sqlite3 sqlite.db .dump > dump.sql

现在,我想将它迁移到我的 Kubernetes 部署中。我已使用以下方式连接到它:

psql -h <external IP of node> -U postgresadmin --password -p <port that i got using kubectl get svc postgres>

到目前为止一切似乎都有效,现在我遇到了导致我有两个问题的问题!

  1. 我已经看到了一些关于将 SQLite 迁移到 PostgreSQL 的问题,并且似乎这个命令可以做到这一点: psql /path/to/psql -d database -U username -W < /the/path/to/dump.sql. 如果转储包含多个表,我是否必须在 PostgreSQL 中创建相同的表(空但具有相同的列等),还是该命令会自动执行此操作?根据我在问题 2 中分享的输出,我相信我会这样做,但它也会自动转到下一张桌子吗?
  2. 如何结合问题1中的命令将其迁移到kubernetes部署?我尝试添加< dump.sql到我用来连接它的命令,但得到以下输出:
CREATE TABLE
INSERT 0 1
ERROR:  relation "first_report" does not exist
ERROR:  current transaction is aborted, commands ignored until end of transaction block
ERROR:  current transaction is aborted, commands ignored until end of transaction block
ERROR:  current transaction is aborted, commands ignored until end of transaction block
ERROR:  current transaction is aborted, commands ignored until end of transaction block

所以,显然我做错了什么......转储包含三个表first_reportsecond_reportrelation_table

标签: postgresqlsqlitekubernetes

解决方案


  1. 你绝对不需要在 PostgreSQL 中创建空表,你的转储文件应该包含create table语句。我建议仅使用转储模式进行测试,而不使用数据sqlite3 sqlite.db .schema > dump.sql

  2. 为了从容器内的转储中恢复数据库,只需运行

    kubectl exec -it postgres-pod -- psql --username admin --d testdb < dump.sql

如果没有帮助,请分享你的 dump.sql文件(仅限模式)


推荐阅读