Exportar tabla PostgreSQL a archivo CSV con encabezados


Estoy tratando de exportar una tabla PostgreSQL con encabezados a un archivo CSV a través de la línea de comandos, sin embargo, puedo exportar a un archivo CSV pero sin encabezados. Necesito esos encabezados también. Mi código se ve como sigue

COPY products_273 to '/tmp/products_199.csv' delimiters',';
Author: Arturo Herrero, 2009-07-13

9 answers

COPY products_273 TO '/tmp/products_199.csv' DELIMITER ',' CSV HEADER;

Como se describe en el manual .

 498
Author: Milen A. Radev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-02 22:41:32

Desde la línea de comandos de psql:

\COPY my_table TO 'filename' CSV HEADER

No hay punto y coma al final.

 172
Author: Laurent Debricon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-24 15:41:47

En lugar de solo el nombre de la tabla, también puede escribir una consulta para obtener solo los datos de columna seleccionados.

COPY (select id,name from tablename) TO 'filepath/aa.csv' DELIMITER ',' CSV HEADER;

Con privilegios de administrador

\COPY (select id,name from tablename) TO 'filepath/aa.csv' DELIMITER ',' CSV HEADER;
 88
Author: Dhruvil Thaker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-02 16:39:15

Cuando no tengo permiso para escribir un archivo desde Postgres encuentro que puedo ejecutar la consulta desde la línea de comandos.

psql -U user -d db_name -c "Copy (Select * From foo_table LIMIT 10) To STDOUT With CSV HEADER DELIMITER ',';" > foo_data.csv
 67
Author: Brian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-01 17:32:00

Esto funciona

psql dbname -F , --no-align -c "SELECT * FROM TABLE"
 31
Author: jordg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-13 06:17:52

Para la versión 9.5 que uso, sería así:

COPY products_273 TO '/tmp/products_199.csv' WITH (FORMAT CSV, HEADER);
 7
Author: maytham-ɯɐɥʇʎɐɯ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-13 00:26:04

He aquí cómo lo conseguí trabajando power shell usando pgsl connnect a una base de datos PG de Heroku:

Primero tuve que cambiar la codificación del cliente a utf8 de la siguiente manera: \encoding UTF8

Luego volcó los datos a un archivo CSV esto:

\copy (SELECT * FROM my_table) TO  C://wamp64/www/spider/chebi2/dump.csv CSV DELIMITER '~'

Usé ~ como delimitador porque no me gustan los archivos CSV, usualmente uso archivos TSV, pero no me permite agregar '\t ' como delimitador, así que usé ~ porque es un personaje raramente usado.

 3
Author: Horse O'Houlihan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-26 19:31:29

Copie (anysql query datawant toexport) a 'fileablsoutepathwihname ' delimiter', ' csv header;

Usando esta u también se pueden exportar datos.

 0
Author: user3767321,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-10 06:44:46

Esta solución funcionó para mí usando \copy.

psql -h <host> -U <user> -d <dbname> -c "\copy <table_name> FROM '<path to csvfile/file.csv>' with (format csv,header true, delimiter ',');"
 0
Author: Atihska,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-04 15:43:46