postgresql-copy

How to ignore errors with psql \copy meta-command

a 夏天 提交于 2019-11-28 00:32:29
问题 I am using psql with a PostgreSQL database and the following copy command: \COPY isa (np1, np2, sentence) FROM 'c:\Downloads\isa.txt' WITH DELIMITER '|' I get: ERROR: extra data after last expected column How can I skip the lines with errors? 回答1: You cannot skip the errors without skipping the whole command up to and including Postgres 9.5. There is currently no more sophisticated error handling. \copy is just a wrapper around SQL COPY that channels results through psql. The manual for COPY

Export specific rows from a PostgreSQL table as INSERT SQL script

守給你的承諾、 提交于 2019-11-27 10:01:49
I have a database schema named: nyummy and a table named cimory : create table nyummy.cimory ( id numeric(10,0) not null, name character varying(60) not null, city character varying(50) not null, CONSTRAINT cimory_pkey PRIMARY KEY (id) ); I want to export the cimory table's data as insert SQL script file. However, I only want to export records/data where the city is equal to 'tokyo' (assume city data are all lowercase). How to do it? It doesn't matter whether the solution is in freeware GUI tools or command line (although GUI tools solution is better). I had tried pgAdmin III, but I can't find

PostgreSQL: export resulting data from SQL query to Excel/CSV

末鹿安然 提交于 2019-11-27 08:18:44
I need to export the resulting data from a query in PostgreSQL to Excel/CSV. I use PostgreSQL 8.2.11 . SQL error: ERROR: relative path not allowed for COPY to file In statement: COPY (select distinct(m_price) from m_product)TO '"c:\auto_new.txt"'; Erwin Brandstetter Example with Unix-style file name: COPY (SELECT * FROM tbl) TO '/var/lib/postgres/myfile1.csv' format csv; Read the manual about COPY (link to version 8.2). You have to use an absolute path for the target file. Be sure to double quote file names with spaces. Example for MS Windows: COPY (SELECT * FROM tbl) TO E'"C:\\Documents and

COPY with dynamic file name

本小妞迷上赌 提交于 2019-11-27 07:44:20
问题 I am trying to write a function to load csv data into a table. I want the input argument to be the path to the file. CREATE OR REPLACE FUNCTION public.loaddata(filepathname varchar) RETURNS void AS $BODY$ BEGIN COPY climatedata( climatestationid, date, prcp, prcpqflag, prcpmflag, prcpsflag, tmax, tmaxqflag, tmaxmflag, tmaxsflag, tmin, tminqflag, tminmflag, tminsflag) FROM $1 WITH csv header; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION public.filltmaxa(character varying)

Importing zipped CSV file into PostgreSQL

情到浓时终转凉″ 提交于 2019-11-27 03:31:25
问题 I have a big compressed csv file (25gb) and I want to import it into PostgreSQL 9.5 version. Is there any fast way to import zip or qzip file into postgres without extracting the file? 回答1: There is an old trick to use a named pipe (works on Unix, don't know about Windows) create a named pipe: mkfifo /tmp/omyfifo write the file contents to it: zcat mycsv.csv.z > /tmp/omyfifo & [from psql] copy mytable(col1,...) from '/tmp/omyfifo' [when finished] : rm /tmp/omyfifo The zcat in the backgound

How to generate a schema from a CSV for a PostgreSQL Copy

痴心易碎 提交于 2019-11-27 01:20:19
问题 Given a CSV with several dozen or more columns, how can a 'schema' be created that can be used in a CREATE TABLE SQL expression in PostgreSQL for use with the COPY tool? I see plenty of examples for the COPY tool, and basic CREATE TABLE expressions, but nothing goes into detail about cases when you have a potentially prohibitive number of columns for manual creation of the schema. 回答1: If the CSV is not excessively large and available on your local machine then csvkit is the simplest solution

How to copy from CSV file to PostgreSQL table with headers in CSV file?

一笑奈何 提交于 2019-11-26 23:57:30
问题 I want to copy a CSV file to a Postgres table. There are about 100 columns in this table, so I do not want to rewrite them if I don't have to. I am using the \copy table from 'table.csv' delimiter ',' csv; command but without a table created I get ERROR: relation "table" does not exist . If I add a blank table I get no error, but nothing happens. I tried this command two or three times and there was no output or messages, but the table was not updated when I checked it through PGAdmin. Is

Export specific rows from a PostgreSQL table as INSERT SQL script

女生的网名这么多〃 提交于 2019-11-26 14:58:59
问题 I have a database schema named: nyummy and a table named cimory : create table nyummy.cimory ( id numeric(10,0) not null, name character varying(60) not null, city character varying(50) not null, CONSTRAINT cimory_pkey PRIMARY KEY (id) ); I want to export the cimory table's data as insert SQL script file. However, I only want to export records/data where the city is equal to 'tokyo' (assume city data are all lowercase). How to do it? It doesn't matter whether the solution is in freeware GUI

How to export table as CSV with headings on Postgresql?

微笑、不失礼 提交于 2019-11-26 14:50:31
I'm trying to export a PostgreSQL table with headings to a CSV file via command line, however I get it to export to CSV file, but without headings. My code looks as follows: COPY products_273 to '/tmp/products_199.csv' delimiters','; Milen A. Radev COPY products_273 TO '/tmp/products_199.csv' WITH (FORMAT CSV, HEADER); as described in the manual . Laurent Debricon From psql command line: \COPY my_table TO 'filename' CSV HEADER no semi-colon at the end. instead of just table name, you can also write a query for getting only selected column data. COPY (select id,name from tablename) TO 'filepath

PostgreSQL: export resulting data from SQL query to Excel/CSV

烂漫一生 提交于 2019-11-26 14:06:37
问题 I need to export the resulting data from a query in PostgreSQL to Excel/CSV. I use PostgreSQL 8.2.11 . SQL error: ERROR: relative path not allowed for COPY to file In statement: COPY (select distinct(m_price) from m_product)TO '"c:\auto_new.txt"'; 回答1: Example with Unix-style file name: COPY (SELECT * FROM tbl) TO '/var/lib/postgres/myfile1.csv' format csv; Read the manual about COPY (link to version 8.2). You have to use an absolute path for the target file. Be sure to double quote file