Last active
December 15, 2015 04:19
-
-
Save quiver/5200984 to your computer and use it in GitHub Desktop.
copy null data(tsv/csv) into postgresql
This file contains hidden or 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
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) |
This file contains hidden or 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
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) |
This file contains hidden or 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
COPY table_name [ ( column_name [, ...] ) ] | |
FROM { 'filename' | STDIN } | |
[ [ WITH ] ( option [, ...] ) ] |
This file contains hidden or 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
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) |
This file contains hidden or 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
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) |
This file contains hidden or 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 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.
This file contains hidden or 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
1 food | |
2 energy | |
3 | |
4 \N |
This file contains hidden or 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
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