简体   繁体   English

如何将数据从CSV转储复制到具有不同结构的PSQL数据库中?

[英]How do I copy data from a CSV dump into a PSQL database with a different structure?

I'm currently trying to copy a SQLite3 database onto a PostgreSQL database with a DIFFERENT structure. 我目前正在尝试将SQLite3数据库复制到具有DIFFERENT结构的PostgreSQL数据库中。 How do I do that? 我怎么做?

What I did so far: 到目前为止,我做了什么:

  1. I dumped the SQLite database and created CSV files with headers for all tables. 我转储了SQLite数据库并创建了带有所有表头的CSV文件。
  2. I then created a new PSQL database with a NEW table structure that suits my needs. 然后,我创建了一个满足我需要的具有新表结构的新PSQL数据库。
  3. I also created a PSQL database with the old SQLite table structure and imported the data from the mentioned CSV files so I have access to both databases, the one with the old schema and the new one, just in case I need it. 我还使用旧的SQLite表结构创建了一个PSQL数据库,并从提到的CSV文件导入了数据,因此我可以访问这两个数据库,一个是旧模式,另一个是新模式,以防万一。

The problem: 问题:

I don't know how to import/copy the data if the table and column names differ from the original ones. 如果表和列的名称与原始名称不同,我不知道如何导入/复制数据。 Eg the SQLite database and the CSV have the following structure for the table „keyword“: 例如,SQLite数据库和CSV对于“关键字”表具有以下结构:

ID, CreatedBy, CreatedByUserId, CreatedOn, ModifiedBy, ModifiedByUserId, ModifiedOn, Name, Notes, Protected, ReservedData

The new PSQL database has the following structure for the differently named table „keywords“: 对于不同名称的表“关键字”,新的PSQL数据库具有以下结构:

KeywordsID, CreatedOn, Keyword, ModifiedOn, Notes

Obviously a simple 显然很简单

COPY keywords(
            keywordsid, createdon, keyword, notes)
FROM '/home/user/Desktop/bib_csv/keywords.csv' DELIMITER ',' CSV;

will not work. 不管用。

Background: 背景:

The original database was created by a bibliographical program called Citavi. 原始数据库由一个名为Citavi的书目程序创建。 It started out as a Microsoft Access database and was later ported to SQLite3 by the program itself, apparently the database now contains some errors that prevent most programs to assist with the export to PSQL, which means I have to do it manually. 它最初是作为Microsoft Access数据库开始的,后来由程序本身移植到SQLite3,显然数据库现在包含一些错误,这些错误阻止大多数程序协助导出到PSQL,这意味着我必须手动进行操作。

I would like to have online access to the database, but for a number of reasons I want it to run on PostgreSQL instead of SQLite. 我希望可以在线访问数据库,但是由于多种原因,我希望它在PostgreSQL而不是SQLite上运行。

You can't import columns selectively with copy . 您无法使用copy选择性地导入列。 If you don't want to use a different tool, the easiest way of doing this is to create a staging table that has the original structure, then COPY into that table and then copy only some columns using an insert statement: 如果您不想使用其他工具,最简单的方法是创建具有原始结构的登台表,然后将COPY到该表中,然后使用insert语句仅复制一些列:

create table keywords_old
(
  ID ..., 
  CreatedBy ..., 
  CreatedByUserId ..., 
  CreatedOn, ...
);

COPY keywords_old
FROM '/home/user/Desktop/bib_csv/keywords.csv' DELIMITER ',' CSV;

insert into keywords (keywordsid, createdon, keyword, notes)
select id, createdon, name, notes
from keywords_old;

Another option is to export only the selected columns from the old database: 另一个选择是仅从旧数据库中导出选定的列:

COPY keywords (id, createdon, name, notes)
  TO '/home/user/Desktop/bib_csv/keywords.csv' DELIMITER ',' CSV;

Then you can import that file into the new database. 然后,您可以将该文件导入到新数据库中。


You can make all that a lot easier, if you import the old data into the same database but into a different schema (eg old_data ) instead of public you can just copy the data using plain insert statements: 如果将旧数据导入同一个 数据库但导入不同的模式(例如old_data )而不是public ,则可以使所有操作变得更加容易,而只需使用普通insert语句复制数据即可:

insert into public.keywords (keywordsid, createdon, keyword, notes)
select id, createdon, name, notes
from old_data.keywords;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM