I have the following command that reads and insert from stdin in a SQL file:
COPY test_table (id, col1, col2) FROM stdin DELIMITER ',' ;
1, "",,
2, "",,
as above the col1 values should be treated as empty string but instead I get an error:
null value in column "col1" violates not null constraint
three , (comma) actually you are inserting 4 fields/columns!
You can try
COPY test_table (id, col1, col2) FROM stdin with (format csv, force_not_null(col1), DELIMITER ',') ;
1,"",
2,"",
\.
or
COPY test_table (id, col1, col2) FROM stdin with (format csv, force_not_null(col1,col2), DELIMITER ',') ;
3,"",
4,"",
\.
Both query will make col1 value as empty string. But the first one will make col2 value to null, the second query will make col2 value as empty string.
Related
begin;
create table test101(col1 int default 2, col2 text default 'hello world');
insert into test101 values (1,default);
insert into test101 values (default,default);
insert into test101 values (default,'dummy');
insert into test101 values (5,'dummy');
commit;
update: OK.
update test101 set col2 = default where col1 = 4;
select, delete not OK.
select * from test101 where col1 = COALESCE (col1,default);
delete from test101 where col1 = COALESCE (col1,default);
error code:
ERROR: 42601: DEFAULT is not allowed in this context
LINE 1: delete from test101 where col1 = COALESCE (col1,default);
^
LOCATION: transformExprRecurse, parse_expr.c:285
also tried: delete from test101 where col1 = default;
default value is not easy to find.
Get the default values of table columns in Postgres? Then select/delete operation with default operation is not that weird.
In the question you linked to they do:
SELECT column_name, column_default
FROM information_schema.columns
WHERE (table_schema, table_name) = ('public', 'test101')
ORDER BY ordinal_position;
which produces something like:
column_name | column_default
-------------+---------------------
col1 | 2
col2 | 'hello world'::text
Maybe, you can combine this query with your query? (But I would not recommend it, because ... )
I have some text file. Which I can't import into a PostgreSQL table because some of my columns have NULL values. My table's columns are of type integer, bigint, double precision.
If all column's data types are character varying, it imports well. When the above data types are used, I get an error.
sample table
create table if not exists test
(
col1 character varying,
col2 bigint default 0,
col3 integer default 0,
col4 double precision default 0.0,
col5 double precision default 0.0,
col6 character varying,
col7 character varying)
sample text file:
1234||||0.0566|ab3|
1234||||0.0566|ab3|
1234||2||0.0566|ab3|
1234|9465662698|||0.0566|ab3|
This is the command I am using:
copy test.test1 (col1, col2, col3, col4, col5, col6, col7) FROM '/home/tanzir/Documents/test' DELIMITER '|'
I get an error like:
ERROR: invalid input syntax for type bigint: ""
CONTEXT: COPY test1, line 1, column col2: ""
Please help me how to solve it?
For csv files the below command should suffice
COPY public.test (col1, col2, col3, col4, col5, col6, col7)
FROM '/home/tanzir/Documents/test' WITH(FORMAT CSV, DELIMITER '|');
For text files use the below command
COPY public.test (col1, col2, col3, col4, col5, col6, col7)
FROM '/home/tanzir/Documents/test' WITH(FORMAT TEXT, DELIMITER '|', NULL '');
The reason for this behaviour is the below:
NULL: Specifies the string that represents a null value. The default is \N (backslash-N) in text format, and an unquoted empty string in CSV format. You might prefer an empty string even in text format for cases where you don't want to distinguish nulls from empty strings. This option is not allowed when using binary format.
source: https://www.postgresql.org/docs/current/sql-copy.html
ps: Since you don't define FORMAT it uses the default which is TEXT
The problem is that you specified the default text format for COPY, where NULL values are represented by \N. Choose the csv format:
COPY test.test1 FROM ... (FORMAT 'csv', DELIMITER '|');
I have following with statement and copy command
with output01 as
(select * from (
select name,
case
when column1 is not null and lower(column1) in ('point1','point2','point3','point4') then 3456
else null end column1Desc,
case
when column2 is not null and lower(column2) in ('point1','point2','point3','point4') then 2456
else null end column2Desc,
column3, column4),
output02 as
(select * from (
select name,
case
when column1 is not null and lower(column1) in ('point1','point2','point3','point4') then 3456
else null end column1Desc,
case
when column2 is not null and lower(column2) in ('point1','point2','point3','point4') then 2456
else null end column2Desc,
column3, column4),
output3 as (SELECT * FROM output01 UNION ALL SELECT * FROM output02)
\copy (select * from output3) to '/usr/share/output.csv' with CSV ENCODING 'UTF-8' DELIMITER ',' HEADER;
I am getting following ERROR
ERROR: relation "tab3" does not exist
All psql backslash commands need to be written on a single line, so you can't have a multi-line query together with \copy. The only workaround is to create a (temporary) view with that query, then use that in the \copy command.
Something along the lines:
create temporary view data_to_export
as
with cte as (..)
select *
from cte
;
\copy (select * data_to_export) to ...
You are getting this error because you are running your CTE query and copy command in different statements. Considering your with query is working fine, you should write your copy statement like below:
\copy (WITH tab1 as (Your SQL statement),
tab2 as ( SELECT ... FROM tab1 WHERE your filter),
tab3 as ( SELECT ... FROM tab2 WHERE your filter)
SELECT * FROM tab3) to '/usr/share/results.csv' with CSV ENCODING 'UTF-8' DELIMITER ',' HEADER;
I have an array (text) (sample row) in my PostgreSQL 9.5 database like follows:
my_array(text)
1,112,292,19.7
I am exporting this text array using Postgres COPY command to a custom text file like this:
Copy
(
Select
my_array
from
my_table
Order by my_table_id
) to '~my_path/output.str' With DELIMITER ',';
I get the output:
1\,112\,292\,19.7\
How can I avoid these unwanted \ in my copy command output?
if the delimiter character (, in your case) is present in a string, it will be escaped (normally by prefixing it with a \)
If you use a different separator (from ,), the , separator doesn't have to be escaped.
If you quote the string in the output, the , separator doesn't have to be escaped.
-- CREATE TABLE my_table(my_table_id SERIAL PRIMARY KEY, my_array text);
-- INSERT INTO my_table(my_array )VALUES ('1,112,292,19.7') ;
COPY ( SELECT my_array FROM my_table ORDER BY my_table_id)
TO '/tmp/output.str'
WITH CSV DELIMITER ',' QUOTE '"'
;
I have the following table with two fields.
Table:
CREATE TABLE str_agg
(
cola varchar(50),
colb varchar(50)
);
Records:
insert into str_agg values('Alex','Student');
insert into str_agg values('Mak','Student');
insert into str_agg values('John','Teacher');
insert into str_agg values('Tony','Teacher');
I want to display the result in the comma separated format like as shown below:
Expected Result:
result
---------------------------------------------------------
Alex(Student),Mak(Student),John(Teacher),Tony(Teacher)
My try:
select string_agg(cola,'('||colb||'),') Result
from str_agg;
Getting result:
result
---------------------------------------------------------
Alex(Student),Mak(Teacher),John(Teacher),Tony
You are passing the value '('||colb||'),' as the delimiter.
You want:
select string_agg(cola||'('||colb||')', ',') Result
from str_agg;