Skip to content

Instantly share code, notes, and snippets.

@seanbehan
Created October 17, 2015 16:49
Show Gist options
  • Save seanbehan/d8d0697d3dbe7eb90550 to your computer and use it in GitHub Desktop.
Save seanbehan/d8d0697d3dbe7eb90550 to your computer and use it in GitHub Desktop.

Revisions

  1. seanbehan renamed this gist Oct 17, 2015. 1 changed file with 0 additions and 0 deletions.
  2. seanbehan created this gist Oct 17, 2015.
    11 changes: 11 additions & 0 deletions bulk-upsert-from-temporary-table
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    create temporary table temp (symbol varchar(255), open decimal, high decimal, low decimal, close decimal, volume varchar(255), date date );

    create table if not exists stocks (id serial primary key, symbol varchar(255), open decimal, high decimal, low decimal, close decimal, volume varchar(255), date date, created_at timestamp, updated_at timestamp);

    copy temp (symbol, date, open, high, low, close, volume) from '/path/to/file.csv' with delimiter ',' csv header;

    delete from stocks using temp where stocks.date = temp.date and stocks.symbol = temp.symbol;

    insert into stocks (symbol, open, high, low, close, volume, date) select symbol, open, high, low, close, volume, date from temp;

    drop table temp;