Created
August 23, 2018 20:57
-
-
Save danielfone/fe8f66e422be5ec75f37d036c59806d5 to your computer and use it in GitHub Desktop.
4-step Postgres CSV Import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Create table to hold raw imported data | |
-- | |
DROP TABLE IF EXISTS imported_reports; | |
CREATE TABLE imported_reports ( | |
reference varchar primary key, | |
project_reference varchar, | |
project_name varchar, | |
version varchar, | |
generation_time varchar, | |
-- etc | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Import data | |
-- | |
COPY imported_reports | |
FROM '/tmp/large-enterprise-export.csv' | |
WITH (format csv, header) | |
; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Load projects from import | |
-- | |
INSERT INTO projects (reference, name, created_at, updated_at) | |
SELECT | |
i.project_reference AS reference, | |
i.project_name AS name | |
now() AS created_at, | |
now() AS updated_at | |
FROM (SELECT DISTINCT project_reference, project_name FROM imported_reports) i | |
ON CONFLICT (reference) DO UPDATE SET | |
name = excluded.name, | |
updated_at = NOW() | |
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSERT INTO reports (reference, program_id, generation_time, version) | |
SELECT | |
i.reference | |
programs.id AS program_id, | |
nullif(i.generation_time, '')::timestamp AS generation_time, | |
nullif(i.version, '')::numeric AS version, | |
-- etc | |
FROM imported_reports i | |
JOIN programs ON programs.reference = i.program_reference | |
ON CONFLICT (external_id) DO UPDATE SET | |
reference = excluded.reference | |
program_id = excluded.program_id, | |
generation_time = excluded.generation_time, | |
version = excluded.version | |
-- etc | |
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TABLE imported_reports; | |
VACUUM FULL ANALYZE VERBOSE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment