psql export query to csv error - postgresql

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 '"'

Related

Problem with postgres \copy command for export query resullt to file

I tried to export a select query result to a csv file. I used Postgres \copy metacommand and command line (psql) to do it. But I got a Syntax error and can't understand why. The Query looks fine to me. Maybe the reason for using metacommand instead of COPY?
The query
\copy
(
SELECT geo_name, state_us_abbreviation, housing_unit_count_100_percent
FROM us_counties_2010
ORDER BY housing_unit_count_100_percent DESC
LIMIT 20
)
TO '/username/Desktop/us_counties_2010_export.csv'
WITH(FORMAT CSV, HEADER, DELIMITER '|');
Error message
ERROR: syntax error at or near "TO"
LINE 7: TO '/username/Desktop/us_counties_2010_export.csv'
\copy is a metacommand given to psql, not a regular command sent to the server. So like other metacommands, the entire \copy command must all be given on one line and doesn't end in a ; but rather a newline.
If you look closely, you will see the first error you got was \copy: arguments required

\copy No such file or directory even though the file exists

I'm trying to copy some data to a csv file via psql's \copy command.
The command i'm trying to use is
\copy (
<some query here>
) to '/Users/tomcaflisch/Downloads/tasks_data.csv' delimiter ',' with csv;
and the error i'm getting is:
/Users/tomcaflisch/Downloads/tasks_data.csv: No such file or directory
I verified the file exists and is writable.
ls -al /Users/tomcaflisch/Downloads/tasks_data.csv
-rwxrwxrwx 1 tomcaflisch staff 0 Jul 10 13:58 /Users/tomcaflisch/Downloads/tasks_data.csv
If I change to `...to '/tmp/tasks_data.csv' delimiter ',' with csv;
I instead get the error
ERROR: syntax error at end of input
LINE 1: ... and t.run_id = r.run_id ) TO STDOUT delimiter ',' with csv;
Well the syntax error is due to this:
to '/Users/tomcaflisch/Downloads/tasks_data.csv' delimiter ',' with csv;
--It should be:
to '/Users/tomcaflisch/Downloads/tasks_data.csv' WITH delimiter ',' csv;
--Or the new style:
to '/Users/tomcaflisch/Downloads/tasks_data.csv' WITH (delimiter ',', format csv);
Try with fixed version to see if it works with /tmp file then /users/* file. If does not work with /Users/* file then crawl up the path to see if there is something wrong with permissions at each level of directory.

Can we apply where clause in postgres Sql

I am using the \copy command for migrating my data . But the table size is 30GB and it is taking hours to migrate. Can I use a where clause so that I can migrate only data that was available a month back?
\copy hotel_room_types TO | (select hotel_room_types.* from hotel_room_types limit 1) $liocation CSV DELIMITER ',';
ERROR: syntax error at or near "."
LINE 1: ...otel_room_types TO STDOUT (select hotel_room_types.* from h...
You can specify a query with psql's \copy just like you can with the SQL command COPY:
\copy (SELECT ... WHERE ...) TO 'filename'
After all, \copy just calls COPY ... TO STDOUT under the hood.

Postgres \copy throws a syntax error at STDOUT when exporting as CSV (v8.0.2)

I'm trying to locally download the results of a query as csv on a postgres instance that I have read-only access to (v8.0.2). I've truly read 10 different ways of going about this and have tried implementing them all (here, here, here, here, and here), but every single time I try to execute the copy command, I get the following error:
ERROR: syntax error at or near "STDOUT"
Here are five of the roughly 20 permutations I have tried. The foo_bar table is a temporary table that was created as the output of a query.
=> \copy "pg_temp_5.foo_bar" TO '/Users/baz/Downloads/foo_bar.csv' DELIMITER ',' CSV
ERROR: syntax error at or near "STDOUT"
LINE 1: COPY "pg_temp_5.foo_bar" TO STDOUT DELIMITER ',' CSV
=> \copy "pg_temp_5.foo_bar" TO '/Users/baz/Downloads/foo_bar.csv' CSV
ERROR: syntax error at or near "STDOUT"
LINE 1: COPY "pg_temp_5.foo_bar" TO STDOUT CSV
=> \copy "pg_temp_5.foo_bar" TO '/Users/baz/Downloads/foo_bar.csv' WITH FORMAT "csv"
ERROR: syntax error at or near "STDOUT"
LINE 1: COPY "pg_temp_5.foo_bar" TO STDOUT WITH FORMAT "csv"
=> \copy pg_temp_5.foo_bar TO '/Users/baz/Downloads/foo_bar.csv' With CSV
ERROR: syntax error at or near "STDOUT"
LINE 1: COPY pg_temp_5.foo_bar TO STDOUT With CSV
=> \copy foo_bar TO foo_bar.csv With CSV
ERROR: syntax error at or near "STDOUT"
LINE 1: COPY foo_bar TO STDOUT With CSV
I figure that it isn't a permissions issue, as if it were, I would be thrown a "permission denied" when I try to run the command. I also know that there have been similar issues with the \copy command as documented by postgres here, but nothing has been noted specifically with regards to my case. Any help would be greatly appreciated!

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