Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would <a href="http://www.postgresql.org/docs/current/interactive/sql-copy.html" rel="noreferrer"><code>COPY</code></a> the file to a temporary table and update the actual table from there. Could look like this:</p> <pre><code>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 </code></pre> <p>If the imported table matches the table to be updated exactly, this may be convenient:</p> <pre><code>CREATE TEMP TABLE tmp_x AS SELECT * FROM tbl LIMIT 0; </code></pre> <p>Creates an empty temporary table matching the structure of the existing table, without constraints.</p> <h3>Privileges</h3> <p>SQL <code>COPY</code> requires superuser privileges for this. (<a href="https://www.postgresql.org/docs/current/static/sql-copy.html" rel="noreferrer">The manual</a>):</p> <blockquote> <p><code>COPY</code> naming a file or command is only allowed to database superusers, since it allows reading or writing any file that the server has privileges to access.</p> </blockquote> <p>The <strong>psql</strong> meta-command <strong><code>\copy</code></strong> works for any db role. <a href="https://www.postgresql.org/docs/current/static/app-psql.html#APP-PSQL-META-COMMANDS-COPY" rel="noreferrer">The manual:</a></p> <blockquote> <p>Performs a frontend (client) copy. This is an operation that runs an SQL <a href="https://www.postgresql.org/docs/current/static/sql-copy.html" rel="noreferrer">COPY</a> 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.</p> </blockquote> <p>The scope of temporary tables is limited to a single <em>session</em> of a single role, so the above has to be executed in the same psql session:</p> <pre><code>CREATE TEMP TABLE ...; \copy tmp_x FROM '/absolute/path/to/file' (FORMAT csv); UPDATE ...; </code></pre> <p>If you are scripting this in a bash command, be sure to wrap it all in a <em>single</em> psql call. Like:</p> <pre><code>echo 'CREATE TEMP TABLE tmp_x ...; \copy tmp_x FROM ...; UPDATE ...;' | psql </code></pre> <p>Normally, you need the meta-command <code>\\</code> to switch between psql meta commands and SQL comands in psql, but <code>\copy</code> is an exception to this rule. The manual again:</p> <blockquote> <p>special parsing rules apply to the <code>\copy</code> meta-command. Unlike most other meta-commands, the entire remainder of the line is always taken to be the arguments of <code>\copy</code>, and neither variable interpolation nor backquote expansion are performed in the arguments.</p> </blockquote> <h3>Big tables</h3> <p>If the import-table is big it may pay to increase <code>temp_buffers</code> temporarily for the session (first thing in the session):</p> <pre><code>SET temp_buffers = '500MB'; -- example value </code></pre> <p>Add an index to the temporary table:</p> <pre><code>CREATE INDEX tmp_x_id_idx ON tmp_x(id); </code></pre> <p>And run <a href="http://www.postgresql.org/docs/current/interactive/sql-analyze.html" rel="noreferrer"><code>ANALYZE</code></a> manually, since temporary tables are not covered by autovacuum / auto-analyze.</p> <pre><code>ANALYZE tmp_x; </code></pre> <p>Related answers: </p> <ul> <li><a href="https://stackoverflow.com/questions/8290900/pg-best-way-to-delete-millions-of-rows-by-id/8290958#8290958">Best way to delete millions of rows by ID</a> </li> <li><a href="https://stackoverflow.com/questions/13316181/how-can-i-insert-common-data-into-a-temp-table-from-disparate-schemas/13317965#13317965">How can I insert common data into a temp table from disparate schemas?</a> </li> <li><a href="https://stackoverflow.com/questions/1746213/how-to-delete-duplicate-entries/8826879#8826879">How to delete duplicate entries?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload