pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Pepe TD Vo <pepevo@yahoo.com>
To: Christopher Browne <cbbrowne@gmail.com>
Cc: Pgsql-admin <pgsql-admin@postgresql.org>
Cc: Pgsql-general <pgsql-general@postgresql.org>
Subject: Re: create batch script to import into postgres tables
Date: Thu, 18 Jun 2020 12:06:18 -0700
Message-ID: <4d5841c4-9a6c-0e20-2c05-c21d3d3942df@aklaver.com> (raw)
In-Reply-To: <1814718409.184531.1592498400894@mail.yahoo.com>
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>
	<CAFNqd5U_9N6pagHYivRxN1hrq-YrE=gD3n3W76fzORojXMZcVg@mail.gmail.com>
	<1761277119.1163490.1592322138731@mail.yahoo.com>
	<732982585.145357.1592493607620@mail.yahoo.com>
	<cd3af30c-41a2-4df1-d41e-6316b692227e@aklaver.com>
	<1814718409.184531.1592498400894@mail.yahoo.com>

On 6/18/20 9:40 AM, Pepe TD Vo wrote:
> I get this part that separates SQL script for import each table,
> 
> (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 
> localhost, port, db, user, and password.  If I set up a script and let 
> it run it won't connect to the postgresql instance.  I want to know how 
> to execute a batch script connect to the database/instance.
> In oracle 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 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

The above should be -f import.sql. AFAIK there is no -i for psql, so 
that should be failing.

> 
> 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";

psql -d production  -U postgres -c 'select count(*) from cell_per';
Null display is "NULL".
  count
-------
     68
(1 row)

psql -d production  -U postgres -c 'select count(*) from cell_per'
Null display is "NULL".
  count
-------
     68

psql -d production  -U postgres -c 'select count(*) from cell_per;'
Null display is "NULL".
  count
-------
     68


> 
> none of them is work. Try to learn how to execute its script.

What error messages do you get?

> 
> **
> *Bach-Nga
> 
> *No one in this world is pure and perfect.  If you avoid people for 
> their mistakes you will be alone. So judge less, love, and forgive 
> more.EmojiEmojiEmoji
> 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 him well, he was a perfect gentleman 
> (Hermione Gingold)
> 
> **Live simply **Love generously **Care deeply **Speak kindly.
> *** Genuinely rich *** Faithful talent *** Sharing success
> 
> 
> 
> 
> On Thursday, June 18, 2020, 12:08:44 PM EDT, Adrian Klaver 
> <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).
> 
>  > I have a Postgresql client installed and connected.  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:
> 
> 
> 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
> the copying the whole copy will rollback. Given that the one file per
> table might be preferable.
> 
> 
>  >
>  > **
>  > *Bach-Nga
> 
>  >
> 
> 
> 
> -- 
> Adrian Klaver
> adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (32+ messages)  latest in thread

Message-ID: <4d5841c4-9a6c-0e20-2c05-c21d3d3942df@aklaver.com>
Permalink:  ../4d5841c4-9a6c-0e20-2c05-c21d3d3942df@aklaver.com/
Also on:    postgresql.org/message-id/4d5841c4-9a6c-0e20-2c05-c21d3d3942df@aklaver.com

 · 

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-admin@postgresql.org
  Cc: adrian.klaver@aklaver.com, pepevo@yahoo.com, cbbrowne@gmail.com, pgsql-general@postgresql.org
  Subject: Re: create batch script to import into postgres tables
  In-Reply-To: <4d5841c4-9a6c-0e20-2c05-c21d3d3942df@aklaver.com>
  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> <CAFNqd5U_9N6pagHYivRxN1hrq-YrE=gD3n3W76fzORojXMZcVg@mail.gmail.com> <1761277119.1163490.1592322138731@mail.yahoo.com> <732982585.145357.1592493607620@mail.yahoo.com> <cd3af30c-41a2-4df1-d41e-6316b692227e@aklaver.com> <1814718409.184531.1592498400894@mail.yahoo.com> <4d5841c4-9a6c-0e20-2c05-c21d3d3942df@aklaver.com>

* 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