Postgres copy with strings enclosed in quotes - postgresql

I have a tab delimited file I am trying to import into Postgres. It looks like this -
"ID" "NBR"
"101931126593" "3"
I can successfully import this file using this command -
\copy service from test.txt with delimiter E'\t' null as 'NULL';
However, this is not omitting the quotes. For example, I want ID to be 101931126593 and not "101931126593".
I have tried this so far, but it still does not import it without the quotes -
\copy service from test.txt with CSV delimiter E'\t' QUOTE E'\b' null as 'NULL';

" is the default quoting character anyway:
\copy service FROM 'test.txt' (FORMAT 'csv', DELIMITER E'\t', NULL 'NULL')

Related

COPY Postgres table with Delimiter as double byte

I want to copy a Postgres (version 11) table into a csv file with delimiter as double byte character. Please assist if this can be achieved.
I am trying this:
COPY "Tab1" TO 'C:\Folder\Tempfile.csv' with (delimiter E'অ');
Getting an error:
COPY delimiter must be a single one-byte character
You could use COPY TO PROGRAM. On Unix system that could look like
COPY "Tabl" TO PROGRAM 'sed -e ''s/|/অ/g'' > /outfile.csv' (FORMAT 'csv', delimiter '|');
Choose a delimiter that does not occur in the data. On Windows, perhaps you can write a Powershell command that translates the characters.

Redshift Uploading Error for quote : Missing newline: Unexpected character 0x22

When trying copy S3 file to Redshift, we're facing the error as follows;
Missing newline: Unexpected character 0x22 found at location 2
0x22 means double quotations, since target data contains "," in some column like 'NO,123456', so we quote each columns by double quotations in exported csv file.
And run the copy command like this;
-- copy command
copy <target_table_name> from 's3://s3-bucket-name'
iam_role 'arn:aws:iam::XXXXXXXXXXXXXXXXXXX' REGION 'ap-northeast-1'
REMOVEQUOTES
ESCAPE
DATEFORMAT 'auto'
TIMEFORMAT 'auto'
DELIMITER ','
ACCEPTINVCHARS '?'
COMPUPDATE TRUE
STATUPDATE TRUE
TRUNCATECOLUMNS
EMPTYASNULL
EXPLICIT_IDS
ENCODING UTF8;
Also tried 'csv' option, instead of removequotes/escape, but it can't work.
I must admit there is something wrong my command, but so far, I have no idea.
Any advice would be highly appreciated.

psql export query to csv error

I'm trying to export the results of my SQL query to a CSV file but it doesn't seem to work. I'm using postgresql version 10.4
\copy (select 1,2) To '/test.csv' With CSV DELIMITER ',';
This gives the follwing error:
ERROR: syntax error at or near "("
LINE 1: COPY ( select 1,2 ) TO STDOUT With CSV DELIMITER ',';
Running the query by itself works fine. I've also tried removing the brackets.
Any help appreciated! Thanks
Above query should work fine. Make sure you write the above query in single line and not multi-line.
This should work.
\copy (select 1,2) to '/test1.csv' with delimiter ',' csv header quote as '"'

SQLite: set quote character exporting CSV

I have a SQL database. If I export it as CSV using sqlitebrowser there is an option to define a "Quote character". How can I set the quote character when doing the export with a script? The default is " and want it to be #.
Export script without quote-character definition:
sqlite3 my.db<<EOF
.mode csv
.separator |
.output out.csv
SELECT * FROM myTable
EOF
Desired output:
#This is#|# a
"table" with#|# three columns, #
# some
newlines,
and...
#|#two#|# "rows". #
I've looked in the documentation und couldn't find such an option. Does it exist?

mass import .csv files into postgresql

i am using Postgresql 9.4 and trying to import all files in a specific folder into and existing table using the following command:
COPY xxx_table FROM '/filepath/filename_*.csv' DELIMITER ',' CSV HEADER;
marking * as a variable part of the file name.
However it results in an error. I have found similar question on here however non of them is related to "COPY" command or alternatively using psql.
Any help on this would be highly appriciated.
Do you need first create the table manually, then:
Copy data from your CSV file to the table:
Example:
\copy zip_codes FROM '/path/to/csv/ZIP_CODES.txt' DELIMITER ',' CSV
You can also specify the columns to read:
Example:
\copy zip_codes(ZIP,CITY,STATE) FROM '/path/to/csv/ZIP_CODES.txt' DELIMITER ',' CSV