Skip to content

Instantly share code, notes, and snippets.

@quiver
Last active December 15, 2015 04:19
Show Gist options
  • Save quiver/5200984 to your computer and use it in GitHub Desktop.
Save quiver/5200984 to your computer and use it in GitHub Desktop.
copy null data(tsv/csv) into postgresql
postgres=# copy t(id, name) from '/tmp/data.csv' (format csv) ;
COPY 5
postgres=# select id , name, length(name) from t;
id | name | length
----+--------+--------
1 | food | 4
2 | energy | 6
3 | '' | 2
4 | | 0
5 | |
(5 rows)
postgres=# copy t(id, name) from '/tmp/data.csv' (format csv, force_not_null(name)) ;
COPY 5
postgres=# select id , name, length(name) from t;
id | name | length
----+--------+--------
1 | food | 4
2 | energy | 6
3 | '' | 2
4 | | 0
5 | | 0
(5 rows)
COPY table_name [ ( column_name [, ...] ) ]
FROM { 'filename' | STDIN }
[ [ WITH ] ( option [, ...] ) ]
postgres=# copy t(id, name) from '/tmp/data.tsv';
COPY 4
postgres=# select id , name, length(name) from t;
id | name | length
----+--------+--------
1 | food | 4
2 | energy | 6
3 | | 0
4 | |
(4 rows)
postgres=# copy t(id, name) from '/tmp/data.tsv' (null '');
COPY 4
postgres=# select id , name, length(name) from t;
id | name | length
----+--------+--------
1 | food | 4
2 | energy | 6
3 | |
4 | N | 1
(4 rows)
create table t(
id integer,
name text
);
postgres=# \d t
Table "public.t"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
name | text |
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 1 column, instead of 2 in line 1.
1 food
2 energy
3
4 \N
1 food
2 energy
3 ''
4
5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment