Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7169743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:04:18+00:00 2026-05-28T15:04:18+00:00

I’m using Postgres and would like to make a big update query that would

  • 0

I’m using Postgres and would like to make a big update query that would pick up from a CSV file, lets say I got a table that’s got (id, banana, apple).

I’d like to run an update that changes the Bananas and not the Apples, each new Banana and their ID would be in a CSV file.

I tried looking at the Postgres site but the examples are killing me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:04:19+00:00Added an answer on May 28, 2026 at 3:04 pm

    COPY the file to a temporary staging table and update the actual table from there. Like:

    CREATE TEMP TABLE tmp_x (id int, apple text, banana text); -- but see below
    
    COPY tmp_x FROM '/absolute/path/to/file' (FORMAT csv);
    
    UPDATE tbl
    SET    banana = tmp_x.banana
    FROM   tmp_x
    WHERE  tbl.id = tmp_x.id;
    
    DROP TABLE tmp_x; -- else it is dropped at end of session automatically
    

    If the imported table matches the table to be updated exactly, this may be convenient:

    CREATE TEMP TABLE tmp_x AS SELECT * FROM tbl LIMIT 0;
    

    Creates an empty temporary table matching the structure of the existing table, without constraints.

    Privileges

    Up to Postgres 10, SQL COPY requires superuser privileges for this.
    In Postgres 11 or later, there are also some predefined roles (formerly "default roles") to allow it. The manual:

    COPY naming a file or command is only allowed to database superusers
    or users who are granted one of the roles pg_read_server_files,
    pg_write_server_files, or pg_execute_server_program […]

    The psql meta-command \copy works for any db role. The manual:

    Performs a frontend (client) copy. This is an operation that runs an
    SQL COPY command, but instead of the server reading or writing the
    specified file, psql reads or writes the file and routes the data
    between the server and the local file system. This means that file
    accessibility and privileges are those of the local user, not the
    server, and no SQL superuser privileges are required.

    The scope of temporary tables is limited to a single session of a single role, so the above has to be executed in the same psql session:

    CREATE TEMP TABLE ...;
    \copy tmp_x FROM '/absolute/path/to/file' (FORMAT csv);
    UPDATE ...;
    

    If you are scripting this in a bash command, be sure to wrap it all in a single psql call. Like:

    echo 'CREATE TEMP TABLE tmp_x ...; \copy tmp_x FROM ...; UPDATE ...;' | psql
    

    Normally, you need the meta-command \\ to switch between psql meta commands and SQL commands in psql, but \copy is an exception to this rule. The manual again:

    special parsing rules apply to the \copy meta-command. Unlike most other meta-commands, the entire remainder of the line is always taken to be the arguments of \copy, and neither variable interpolation nor backquote expansion are performed in the arguments.

    Big tables

    If the import-table is big it may pay to increase temp_buffers temporarily for the session (first thing in the session):

    SET temp_buffers = '500MB';  -- example value
    

    Add an index to the temporary table:

    CREATE INDEX tmp_x_id_idx ON tmp_x(id);
    

    And run ANALYZE manually, since temporary tables are not covered by autovacuum / auto-analyze.

    ANALYZE tmp_x;
    

    Related answers:

    • Best way to delete millions of rows by ID
    • How can I insert common data into a temp table from disparate schemas?
    • How to delete duplicate entries?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.