Raja's Exocortex

Importing/exporting Postgres/CSV

Postgresql csv to db translate foreign keys

  1. Import raw CSV into temporary table
  2. Create master tables from temporary table by SELECT DISTINCT
  3. Create data table from temporary table by joining PK/FK with master table
-- Temporary table for importing raw CSV
create table tmp (
    id     serial PRIMARY KEY,
    topic  varchar(255),
    format varchar(255),
    text   text
);


-- Master tables
CREATE TABLE topics(
    topic_id serial      PRIMARY KEY,
    topic    varchar(50) NOT NULL
);

CREATE TABLE formats(
    format_id serial      PRIMARY KEY,
    format    varchar(50) NOT NULL
);

-- Actual data table
CREATE TABLE texts(
    text_id   serial  PRIMARY KEY,
    topic_id  integer REFERENCES topics,
    format_id integer REFERENCES formats,
    text      text
);

-- psql client command to copy CSV to tmp table
\COPY tmp(topic, format, text) FROM 'test.csv' DELIMITER ',' CSV HEADER;

-- Create master tables
INSERT INTO topics (topic) SELECT DISTINCT topic FROM tmp;
INSERT INTO formats (format) SELECT DISTINCT format FROM tmp;

-- Create the final data table, join _id from master tables
INSERT INTO texts (topic_id, format_id, text)
  SELECT b.topic_id, c.format_id, a.text
  FROM tmp a
  JOIN topics b ON a.topic = b.topic
  JOIN formats c ON a.format = c.format;

-- Clean up
DROP TABLE tmp;

References

#postgres #csv