Migrate data to TimescaleDB from the same PostgreSQL instance
Migrate data into a TimescaleDB hypertable from a regular PostgreSQL table
You can migrate data into a TimescaleDB hypertable from a regular PostgreSQL table. This method assumes that you have TimescaleDB set up in the same database instance as your existing table.
Prerequisites
Section titled “Prerequisites”Before beginning, make sure you have installed and set up TimescaleDB.
You also need a table with existing data. In this example, the source table is
named old_table. Replace the table name with your actual table name. The
example also names the destination table new_table, but you might want to use
a more descriptive name.
Migrate data
Section titled “Migrate data”Migrate your data into TimescaleDB from within the same database.
- Call
CREATE TABLEto make a new table based on your existing tableYou can create your indexes at the same time, so you don’t have to recreate them manually. Or you can create the table without indexes, which makes data migration faster.
CREATE TABLE new_table (LIKE old_table INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES) WITH (tsdb.hypertable,tsdb.partition_column='<the name of the time column>');CREATE TABLE new_table (LIKE old_table INCLUDING DEFAULTS INCLUDING CONSTRAINTS EXCLUDING INDEXES) WITH (tsdb.hypertable,tsdb.partition_column='<the name of the time column>');When you create a hypertable using
CREATE TABLE ... WITH ..., the default partitioning column is automatically the first column with a timestamp data type. Also, TimescaleDB creates a columnstore policy that automatically converts your data to the columnstore, after an interval equal to the value of the chunk interval, defined throughcompress_afterin the policy. This columnar format enables fast scanning and aggregation, optimizing performance for analytical workloads while also saving significant storage space. In the columnstore conversion, hypertable chunks are compressed by up to 98%, and organized for efficient, large-scale queries.You can customize this policy later using alter_job. However, to change
afterorcreated_before, the compression settings, or the hypertable the policy is acting on, you must remove the columnstore policy and add a new one.You can also manually convert chunks in a hypertable to the columnstore.
- Insert data from the old table to the new tableINSERT INTO new_tableSELECT * FROM old_table;
- Recreate indexes if needed
If you created your new table without indexes, recreate your indexes now.