lunes, 3 de octubre de 2016

Postgres - export/import en formato CSV


Export:

en windows:
COPY tablename(field_x, field_y, ..) to '\path\to\copy\file.csv' delimiters ',' csv header;

ó  (Encoding si dificultades con acentos y caracteres especiales)

COPY  tablename  
TO 'c:\path\to\copy\file.csv'
WITH (FORMAT csv, HEADER, ENCODING 'WIN1252' )

COPY (select field_x, field_y  from tablename where condition)
TO 'c:\path\to\copy\file.csv'
WITH (FORMAT csv, HEADER, ENCODING 'WIN1252' )


en linux:
COPY tablename(field_x, field_y, ..) TO '/path/to/copy/file.csv' delimiters ',' csv header;

Import (previamente se debe crear tabla con la estructura de campos requerida):

en linux:
\COPY tablename FROM ~/path/to/file.csv delimiter ',' csv header

Si errores en encoding:
\COPY tablename FROM ~/path/to/file.csv delimiter ',' csv header encoding 'WIN1252'

No hay comentarios.:

Publicar un comentario