• Ivy Seo

Create Hyper-table

Language: C#


pre-requisite: Need a table to copy from


Code:

private static void Create_NewTable_HyperTable()
        {
            // connect to postgresql
            var cs = "Host=##; Username=##; Password=##; Database=##";
            using var con = new NpgsqlConnection(cs);
            con.Open();
            using var cmd = new NpgsqlCommand();
            cmd.Connection = con;
            // CREATE NEW TABLE and COPY from OLD TABLE
            cmd.CommandText = "CREATE TABLE New_Table (LIKE Original_Table INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES)";
            cmd.ExecuteNonQuery();
            cmd.CommandTimeout = 300;   // extended time out 
            // create hypertable and time for the dataset
            cmd.CommandText = "SELECT create_hypertable('New_Table', 'timestamp_')";
            cmd.ExecuteNonQuery();
            // INSERT QUERY : Insert data from old table
            cmd.CommandText = "INSERT INTO New_Table SELECT * FROM Original_Table;";
            cmd.ExecuteNonQuery();
        }