![](/img/trans.png)
[英]How can I migrate data from one PostgreSQL database to another (with slightly different table/column names)?
[英]Copy data from one table to another with different column names and column ordering in timescaledb
我有 2 個 timescaledb 表,它們的列順序不同,有些甚至有不同的列名。 我可以將數據從一個表復制到另一個表嗎?
本質上,新表上有 hyper table,而舊表上沒有 hyper table。
我看過https://docs.timescale.com/timescaledb/latest/how-to-guides/migrate-data/same-db/但是它似乎只告訴我需要具有相同的列名甚至是同一列創建表語法中的順序。 你能幫忙嗎,我是 timescaledb 的新手
例如:
**table1**
id
price
datetime_string
**table2**
id
time
price
我在這里創建了一個最小的例子:
CREATE TABLE old_table ( id bigserial, time_string text NOT NULL, price decimal);
CREATE TABLE new_table ( time TIMESTAMP NOT NULL, price decimal);
SELECT create_hypertable('new_table', 'time');
INSERT INTO old_table (time_string, price) VALUES
('2021-08-26 10:09:00.01', 10.1),
('2021-08-26 10:09:00.08', 10.0),
('2021-08-26 10:09:00.23', 10.2),
('2021-08-26 10:09:00.40', 10.3);
INSERT INTO new_table SELECT time_string::timestamp as time, price from old_table;
結果:
playground=# \i move_data.sql
CREATE TABLE
CREATE TABLE
┌─────────────────────────┐
│ create_hypertable │
├─────────────────────────┤
│ (19,public,new_table,t) │
└─────────────────────────┘
(1 row)
INSERT 0 4
INSERT 0 4
playground=# table new_table;
┌────────────────────────┬───────┐
│ time │ price │
├────────────────────────┼───────┤
│ 2021-08-26 10:09:00.01 │ 10.1 │
│ 2021-08-26 10:09:00.08 │ 10.0 │
│ 2021-08-26 10:09:00.23 │ 10.2 │
│ 2021-08-26 10:09:00.4 │ 10.3 │
└────────────────────────┴───────┘
(4 rows)
你可以先嘗試執行你的 select 看看關系是否匹配表結構?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.