【问题标题】:How to turn a sqlite3 table to a postgresql table?如何将 sqlite3 表转换为 postgresql 表?
【发布时间】:2012-03-07 16:04:58
【问题描述】:

我在不知道 Heroku 需要 Postgresql 的情况下使用 Rails 开始了一个项目。现在我想将我的 sqlite3 表转换为 postgresql 表。我该怎么做?

我找到了一种获取 SQL 插入的方法:

BEGIN TRANSACTION;
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('0', 'bac à papier', NULL, 'bac de recyclage', '13309980782012-03-06 03:57:21.219233', '13309980782012-03-06 03:57:21.219233');
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('1', 'bac PVM', 'plastique/verre/métal', NULL, '13310076572012-03-06 03:57:21.219233', '13310076572012-03-06 03:57:21.219233');
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('2', 'bac de déchets', NULL, 'poubelle', '13310076572012-03-06 03:57:21.219233', '13310076572012-03-06 03:57:21.219233');
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('3', 'cafétéria', '', '', '2012-03-06 03:57:21.219233', '2012-03-06 03:57:21.219233');
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('4', 'compost', NULL, NULL, '2012-03-06 03:57:21.219233', '2012-03-06 03:57:21.219233');
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('5', 'consigne', NULL, NULL, '2012-03-06 03:57:21.219233', '2012-03-06 03:57:21.219233');
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('6', 'CRD', NULL, NULL, '2012-03-06 03:57:21.219233', '2012-03-06 03:57:21.219233');
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('7', 'ressources matérielles', NULL, NULL, '2012-03-06 03:57:21.219233', '2012-03-06 03:57:21.219233');
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('8', 'sécurité', NULL, NULL, '2012-03-06 03:57:21.219233', '2012-03-06 03:57:21.219233');
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('10', 'technicien chimie', NULL, NULL, '2012-03-06 03:57:21.219233', '2012-03-06 03:57:21.219233');
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('11', 'technicien biologie', NULL, NULL, '2012-03-06 03:57:21.219233', '2012-03-06 03:57:21.219233');
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('12', 'service informatique', NULL, NULL, '2012-03-06 03:57:21.219233', '2012-03-06 03:57:21.219233');
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('13', 'îlot multi-récupération', 'face au B-227', NULL, '2012-03-06 03:57:21.219233', '2012-03-06 03:57:21.219233');
insert into places ("id", "name", "content", "tags", "created_at", "updated_at") values ('14', 'récupérateurs extérieurs', NULL, NULL, '2012-03-06 03:57:21.219233', '2012-03-06 03:57:21.219233');
COMMIT;

解决方案 这是我解决问题的方法:

我使用 Chris 的解决方案告诉 rails 我想使用 PostgreSQL。然后,我跑了db:migrate

为了将我的 SQLite 表转换为 PostgreSQL 表,我使用了 Sliteman,这是一个用于管理 SQLite 数据库的 Linux 程序。使用此实用程序,我能够将表转换为 SQL 插入。我登录到 PostgreSQL 并应用了插入。

瞧!

【问题讨论】:

    标签: ruby-on-rails postgresql


    【解决方案1】:

    如果您只想让本地数据库在 postgres 上运行以匹配 heroku,只需将您的 database.yml 更改为如下所示:

    development:
      adapter: postgresql
      encoding: utf8
      database: development
      pool: 5
      username: postgres
      password:
    
    test: &TEST
      adapter: postgresql
      encoding: utf8
      database: test
      pool: 5
      username: postgres
      password:
    
    production:
      adapter: postgresql
      encoding: utf8
      database: production
      pool: 5
      username: postgres
      password:
    
    cucumber:
      <<: *TEST
    

    这是假设您拥有名为“postgres”的 postgres 角色。如果您使用的是 Mac,则默认情况下您的用户名应该是您的用户短名。

    【讨论】:

      猜你喜欢
      • 2021-07-17
      • 1970-01-01
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 2010-10-06
      • 2021-05-16
      • 2014-06-26
      相关资源
      最近更新 更多