Re: create batch script to import into postgres tables - Mailing list pgsql-admin

From Pepe TD Vo
Subject Re: create batch script to import into postgres tables
Date
Msg-id 889466258.267016.1592510085000@mail.yahoo.com
Whole thread Raw
In response to Re: create batch script to import into postgres tables  (Adrian Klaver <adrian.klaver@aklaver.com>)
Responses Re: create batch script to import into postgres tables
Re: create batch script to import into postgres tables
List pgsql-admin
>>psql -d production  -U postgres -c 'select count(*) from cell_per';
Null display is "NULL".
  count
-------
    68
(1 row)

you can do this once you are in psql. But if you are running from shell script, it will be an error

>>What error messages do you get?

my shell script is:
@echo off 
C:\Program Files\PostgreSQL\11\bin\psql -U PSmasteruser  -d PSCIDR -h hostname.amazonaws.com -p 5432 -c "select count(*) from tableA;"
pause

the error I have is 'C:\Program' is not recognized as an internal or external command, operable program or batch file.

I even surround the path in quotes because of space.
@echo off 
cmd /c ""C:\Program Files\PostgreSQL\11\bin\psql" -U PSmasteruser -d PSCIDR -h hostname.amazonaws.com -p 5432 -c "select count(*) from tableA;"
pause

error is: 'C:\Program Files\PostgreSQL\11\bin\psql: illegal option -- u

when I put -P Password123 (or fully qualified password=Password123)

it gives me another error "pset: unknow option: Password123
psql: could not set printing parameter "Password123"

I can connect from psql shell fine when it prompt hostname, username (but connect to postgres not to PSmasteruser, PSmasteruser username set up for aws maintenance postgres database, someone did, not me and the password is same on both).  Once I'm in psql and I can change to PSCIDR instance fine and run select count(*) from tableA;



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, 03:06:39 PM EDT, Adrian Klaver <adrian.klaver@aklaver.com> wrote:


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


pgsql-admin by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: create batch script to import into postgres tables
Next
From: Adrian Klaver
Date:
Subject: Re: create batch script to import into postgres tables