Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Postgresql COPY TO STDIN With CSV do on conflic do update?

I want to do

 " on conflict (time) do update set name , description "

but I have no idea when I use stdin with csv , I don't know what name equal what? and description equal what...

table_a:

enter image description here

xxx.csv:

enter image description here

with open('xxx/xxx.csv', 'r', encoding='utf8') as f:
    sql = """
    COPY table_a FROM STDIN With CSV on conflict (time) 
    do update set name=??, description=??;
    """
    cur.copy_expert(sql, f)
    conn.commit()
like image 200
Frank Liao Avatar asked Dec 29 '17 07:12

Frank Liao


People also ask

How do I convert PostgreSQL data to CSV?

The easiest but the most efficient way to export data from a Postgres table to a CSV file is by using the COPY command. COPY command generates a CSV file on the Database Server. You can export the entire table or the results of a query to a CSV file with the COPY TO command.

Does Postgres COPY overwrite?

If you COPY data into a table already containing data, the new data will be appended. If you COPY TO a file already containing data, the existing data will be overwritten.

Which PostgreSQL command can be used to export table data to a .CSV file?

Psql \copy command is used when you want to export the data from Postgres table to a CSV file on a client machine. To use this command, you will need access to the psql prompt. You will understand it more with the following psql copy examples. To copy the entire table to a csv file, use \copy.

What is Stdin in PostgreSQL?

Stdin: It is a standard input stream. It is used where the program reads the input data. Stdout: This implies the standard output stream used when the application writes the data (output) to the file.


1 Answers

In this SO post, there are two answers that -combined together- provide a nice solution for successfully using ON CONFLICT. The example below, uses ON CONFLICT DO NOTHING;:

CREATE TEMP TABLE tmp_table 
(LIKE main_table INCLUDING DEFAULTS)
ON COMMIT DROP;

COPY tmp_table FROM 'full/file/name/here';

INSERT INTO main_table
SELECT *
FROM tmp_table
ON CONFLICT DO NOTHING;

Replace both instances of main_table with the name of your table.

like image 95
raratiru Avatar answered Sep 21 '22 03:09

raratiru