pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@lists.postgresql.org
Subject: Re: copy file from a client app to remote postgres isntance
Date: Wed, 9 Nov 2022 17:15:42 +0100
Message-ID: <20221109161542.63f3b2q5is5ghass@hjp.at> (raw)
In-Reply-To: <CAGVmuwoKkos2ssA7-MNpsQkxO=0ouOGS9PtqzFViJ2+0SDeXkQ@mail.gmail.com>
References: <CAGVmuwoKkos2ssA7-MNpsQkxO=0ouOGS9PtqzFViJ2+0SDeXkQ@mail.gmail.com>

On 2022-11-07 19:57:04 +0300, Вадим Самохин wrote:
> I have an application that must copy a local file in csv format to a postgres
> table on a remote host. The closest solution is this one (https://
> stackoverflow.com/a/9327519/618020). It boils down to specifying a \copy
> meta-command in a psql command:
> 
> psql -U %s -p %s -d %s -f - <<EOT\n here hoes a \copy meta-command \nEOT\n
> 
> and executing it. But it's quite an unnatural way to write database
> code.

This looks like something you would use in another programming language
(maybe C or Python?) to construct a shell command.

Do you want do this once (from the shell) or from code?

If the former, starting psql and typing

\copy table_name from 'filename.csv' ...

Doesn't seem that unnatural to me.
(That just invokes COPY ... FROM STDIN on the server and feeds data to
it over the existing SQL connection.)

If it's the latter, your programming language's postgresql library
probably has a method for invoking copy.


> Has anything changed in the last ten years? Or, is there a
> better way to copy file contents in a remote database?

COPY is the fastest way to load data.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (11+ messages)  latest in thread

Message-ID: <20221109161542.63f3b2q5is5ghass@hjp.at>
Permalink:  ../20221109161542.63f3b2q5is5ghass@hjp.at/
Also on:    postgresql.org/message-id/20221109161542.63f3b2q5is5ghass@hjp.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: hjp-pgsql@hjp.at
  Subject: Re: copy file from a client app to remote postgres isntance
  In-Reply-To: <20221109161542.63f3b2q5is5ghass@hjp.at>
  References: <CAGVmuwoKkos2ssA7-MNpsQkxO=0ouOGS9PtqzFViJ2+0SDeXkQ@mail.gmail.com> <20221109161542.63f3b2q5is5ghass@hjp.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox