Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jlxaI-0005nk-CN for pgsql-general@arkaria.postgresql.org; Thu, 18 Jun 2020 16:40:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jlxaH-00073E-AO for pgsql-general@arkaria.postgresql.org; Thu, 18 Jun 2020 16:40:13 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jlxaG-00072q-3s for pgsql-general@lists.postgresql.org; Thu, 18 Jun 2020 16:40:13 +0000 Received: from sonic314-13.consmr.mail.bf2.yahoo.com ([74.6.132.123]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jlxaC-0000uU-Fy for pgsql-general@postgresql.org; Thu, 18 Jun 2020 16:40:11 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1592498406; bh=hC5EHdnXCY0ficbwh9xIw/lcyEKGlZ6lH+i2Ejvb2L0=; h=Date:From:Reply-To:To:Cc:In-Reply-To:References:Subject:From:Subject; b=QZYeEAwBT3xI9e0pvVDYYvAnse+aWVP7SeGP2soonTdzUdSv3Okd3RKgnCi3wchU7Qov6tpW9CvkBry/7gahV6jkAJvV+a7Y+kkA0xUVfYofjiZI8KlS/XIykc8b8Fbsjfj4+ioWzHsPC2yCmE/tcwzJFnJYug4hdiD6WZXfggBkXC9hZnI48LeYgoFwDx/1QY+ovk8DYFZaUl3KwMCQ6tAPq4Vb5ci2UL4mfqDXGRNf8OS2xv0FSxy13rFuRl/8a2cCovUjIBnV45ejqQpvLl2qaA4juJ/u9i7BdypOMOCANz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sQyeo5ytYDCsAIXQB5bhpCuBQXKNbwBQ axRWgNyLxHIRm1X9N6U7knwcwL6zLBKX8Q_mL3MMmsSEtxiAGms1cyVD0jNl._N_3Fhchkq1gPrg CG6A9HFNOxsXs0cg4qgxFRJxSgoIQU.Ta4wU3grOIFHBUem6gPltuv6_zvR.I9xlrM873bhhFaUL UlK7CJYdZkxI03j4SGpSqmpfQyucirCvhlBxwgx3AxjrDUaJZpR5cQTMNvq.NmQ.87zhixngiGgG jblARzjcziV3p35RKQzolSZYZS8CD9Ica09NahN4iIDj69wh60f5_2SJLRwwxSLf7ydJ_haUMDOr setwnNBW3G4kGMyIx8VW_XT951BqCRK5wd5zkcOeXkO2Z3MkWtDdPvsB6tCKpW.MMTieM2ipjr8n AM0PwkET18TPmrM.U6be5iHsIesdaVlk8JNc_kb_u Received: from sonic.gate.mail.ne1.yahoo.com by sonic314.consmr.mail.bf2.yahoo.com with HTTP; Thu, 18 Jun 2020 16:40:06 +0000 Date: Thu, 18 Jun 2020 16:40:00 +0000 (UTC) From: Pepe TD Vo Reply-To: Pepe TD Vo To: Christopher Browne , Adrian Klaver Cc: Pgsql-admin , Pgsql-general Message-ID: <1814718409.184531.1592498400894@mail.yahoo.com> In-Reply-To: References: <866581879.1147164.1592317256828.ref@mail.yahoo.com> <866581879.1147164.1592317256828@mail.yahoo.com> <1f5f4fd6-ad22-4823-c922-17a021c2c0d7@aklaver.com> <74046808.1129536.1592317823808@mail.yahoo.com> <3625e291-8456-3831-6205-2d73232bcd1e@aklaver.com> <1718146430.1151531.1592319564343@mail.yahoo.com> <1761277119.1163490.1592322138731@mail.yahoo.com> <732982585.145357.1592493607620@mail.yahoo.com> Subject: Re: create batch script to import into postgres tables MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_184530_2010740720.1592498400892" X-Mailer: WebService/1.1.16138 YMailNorrin Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Content-Length: 13046 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_184530_2010740720.1592498400892 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable I get this part that separates SQL script for import each table,=C2=A0 (import.sql)begin; \copy table_1 (c1, c2, c3) from '/path/tabledata1.csv' csv header; commit; but when open the psql sql shell script it prompts line by line for localho= st, port, db, user, and password.=C2=A0 If I set up a script and let it run= it won't connect to the postgresql instance.=C2=A0 I want to know how to e= xecute a batch script connect to the database/instance.=C2=A0 =C2=A0In orac= le I created a shell script with all oracle_sid, oracle_home, and read the = function/procedure... for psql, especially from window client, I did put ps= ql_home and connect to the instance, it failed c:\Progra~1\PostgreSQL\11\bin\psql -d PSCIDR -h localhost -p 5432 -U postgr= es -i import.sql even I do a simple count c:\Progra~1\PostgreSQL\11\bin\psql -d PSCIDR -h localhost -p 5432 -U postgr= es -c "select count(*) from tableA"; none of them is work. Try to learn how to execute its script. Bach-Nga No one in this world is pure and perfect.=C2=A0 If you avoid people for the= ir mistakes you will be alone. So judge less, love, and forgive more.To cal= l him a dog hardly seems to do him justice though in as much as he had four= legs, a tail, and barked, I admit he was, to all outward appearances. But = to those who knew him well, he was a perfect gentleman (Hermione Gingold) **Live simply **Love generously **Care deeply **Speak kindly.*** Genuinely = rich *** Faithful talent *** Sharing success =20 On Thursday, June 18, 2020, 12:08:44 PM EDT, Adrian Klaver wrote: =20 =20 On 6/18/20 8:20 AM, Pepe TD Vo wrote: Please don't top post. The preferred style on this list is inline or=20 bottom posting(https://en.wikipedia.org/wiki/Posting_style). > I have a Postgresql client installed and connected.=C2=A0 how can i creat= e a=20 > batch script running from the client window? Create a file with commands in it like the example from Christopher=20 Browne that was posted earlier: "There is no single straightforward answer to that. Supposing I want a batch to either all be processed, or to all not process, then I might write a sql file like: begin; \copy table_1 (c1, c2, c3) from '/path/tabledata1.csv' csv header; \copy table_2 (c1, c2, c3) from '/path/tabledata2.csv' csv header; \copy table_3 (c1, c2, c3) from '/path/tabledata3.csv' csv header; commit; But you may be fine with having a separate SQL script for each table. There will be conditions where one or the other is more appropriate, and that will be based on the requirements of the process." Then point psql at it: psql -d some_db -h some_host -U some_user -f the_file Be aware that \copy is all or nothing. If there is a single failure in=20 the copying the whole copy will rollback. Given that the one file per=20 table might be preferable. >=20 > ** > *Bach-Nga >=20 --=20 Adrian Klaver adrian.klaver@aklaver.com =20 ------=_Part_184530_2010740720.1592498400892 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I get this part that separates SQ= L script for import each table, 

(import.sql)
<= div dir=3D"ltr" data-setdir=3D"false">
begin;
\copy table_1 (c1, c2, c3) from '/path/tab= ledata1.csv' csv header;
commit;

but when open the psql sql shell s= cript it prompts line by line for localhost, port, db, user, and password.&= nbsp; If I set up a script and let it run it won't connect to the postgresq= l instance.  I want to know how to execute a batch script connect to t= he database/instance.   
In oracle I created a shell script with all oracle_sid, oracle_home, an= d read the function/procedure... for psql, especially from window client, I= did put psql_home and connect to the instance, it failed

c:\Progra~1\PostgreSQL\11\bin\psql -d PSCIDR -h localhost -p 5432 -U postgres -i import.sql

even I do a simple count

c:\Progra~1\PostgreSQL\11\bin\psql -d PSCIDR -h localhost -p= 5432 -U postgres -c "select count(*) from tableA";
=

none of them is work. Try to learn how to exe= cute its script.

Ba= ch-Nga

No one in this world is pur= e and perfect.  If you avoid people for their mistakes you will be alo= ne. So judge less, love, and forgive more.3D"Emoji=3D"Emoji"3D"=
To call him a dog hardly = seems to do him justice though in as much as he had four legs, a tail, and = barked, I admit he was, to all outward appearances. But to those who knew h= im well, he was a perfect gentleman (Hermione Gingold)=

**Live simply **Love generousl= y **Care deeply **Speak kindly.
= *** Genuine= ly rich *** Faithful talent *** Sharing success

<= span>


=20
=20
On Thursday, June 18, 2020, 12:08:44 PM EDT, Adrian Kla= ver <adrian.klaver@aklaver.com> wrote:


On 6/18/20 8:20 AM, Pepe TD Vo wrote:=

Please don't top post. The preferred = style on this list is inline or
bottom posting(https://en.wikipedia.org/wiki/Posting_style).
<= br clear=3D"none">> I have a Postgresql client installed and connected.&= nbsp; how can i create a
> batch script running from = the client window?

Create a file with = commands in it like the example from Christopher
Browne = that was posted earlier:

"There is no = single straightforward answer to that.


Supposing I want a batch to either all be processed, or= to all not process,
then I might write a sql file like:<= br clear=3D"none">

begin;
\copy table_1 (c1, c2, c3) from '/path/tabledata1.csv' csv header;\copy table_2 (c1, c2, c3) from '/path/tabledata2.csv' csv = header;
\copy table_3 (c1, c2, c3) from '/path/tabledata3= .csv' csv header;
commit;


But you may be fine with having a separate SQL scr= ipt for each table.


There will be conditions where one or the other is more appropriate, andthat will be based on the requirements of the process."
Then point psql at it:

psql -d some_db -h some_host -U some_user -f the_file
Be aware that \copy is all or nothing. = If there is a single failure in
the copying the whole co= py will rollback. Given that the one file per
table migh= t be preferable.


>
> **
> *Bach-Nga

>



--
Adria= n Klaver
adrian.klaver@akla= ver.com



------=_Part_184530_2010740720.1592498400892--