Re: running \copy through perl dbi ?

From: Chris Travers <chris(dot)travers(at)gmail(dot)com>
To: Vincent Veyron <vincent(dot)veyron(at)libremen(dot)org>
Cc: David Gauthier <dfgpostgres(at)gmail(dot)com>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: running \copy through perl dbi ?
Date: 2023-12-11 10:00:17
Message-ID: CAKt_Zftua=bJCR=XzG4L+GWn2be+DBVtYvix0LDiKBzarAFC_A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

\copy in psql just wraps PostgreSQL's COPY FROM STDIN.

if you are trying to do it from your own client program it is trivial to
change to that call instead.

On Mon, Dec 11, 2023 at 4:09 PM Vincent Veyron <vincent(dot)veyron(at)libremen(dot)org>
wrote:

> On Fri, 8 Dec 2023 10:45:28 -0500
> David Gauthier <dfgpostgres(at)gmail(dot)com> wrote:
> >
> > I'm trying to run a PG client side "\copy" command from a perl script. I
> > tried using $dbh->do("\\copy ...") but it barffed when it saw the '\'...
> > ERROR: syntax error at or near "\"
> >
> > I can do this with a command line approach, attaching to the DB then run
> > using...
>
> Duh! I just realized that what I proposed with system() is a command line
> approach.
>
> As David Johnston mentionned, you can use the SQL COPY command.
>
> However, you need then to deal with permissions so that the server may
> write the file, so I wonder what approach is cleaner?
>

I wouldn't do COPY FROM FILE in that case. I would do COPY FROM STDIN and
hten write the data.

Here's the general docs in the DBD::Pg module:
https://metacpan.org/pod/DBD::Pg#COPY-support

The general approach is to COPY FROM STDIN and then use pg_putcopydata for
each row, and finally pg_putcopyend to close out this. It's not too
different from what psql does in the background.

>
>
> --
>
> Bien à vous, Vincent Veyron
>
> https://marica.fr
> Logiciel de gestion des contentieux juridiques, des contrats et des
> sinistres d'assurance
>
>
>
>

--
Best Wishes,
Chris Travers

Efficito: Hosted Accounting and ERP. Robust and Flexible. No vendor
lock-in.
http://www.efficito.com/learn_more

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ian Lawrence Barwick 2023-12-11 11:57:57 Re: Assistance Needed: Error during PostgreSQL Configuration
Previous Message Dominique Devienne 2023-12-11 09:41:39 Re: Question on overall design