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 1jlzrp-0002Qw-Hu for pgsql-admin@arkaria.postgresql.org; Thu, 18 Jun 2020 19:06:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jlzrm-00044S-K5 for pgsql-admin@arkaria.postgresql.org; Thu, 18 Jun 2020 19:06:26 +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 1jlzrl-00044G-DT; Thu, 18 Jun 2020 19:06:26 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jlzri-0002Fm-LU; Thu, 18 Jun 2020 19:06:24 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id B9F0C9F5; Thu, 18 Jun 2020 15:06:20 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Thu, 18 Jun 2020 15:06:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=g 9+BMs0XSRU+TEL5autFFTSfwgP9oriaX4KRr7T+ljM=; b=NeMqOBXKnnufuo3WA nuXbXksAnGF4qje8nq++qCUwywwKRJrWlMmknxjeyRCu7LZ0DsSPEmBx7iCGsJn/ P9esHkh65lrbmECL/QD9X30ZdxaRegdrOepW/iSo4FPDPicB63n0evWvBZMosl8k 6QFA0/ntkCG9Z75VC5/bJL5HOSILZBWF9Yeu/U96kMG0VQ6CpJQJnnCzSeNQ7ZEW MfO3s1pbr8PT92r3/8BfYSeEz3jxWMFFgXRXmZl3CLpNK6eywfubgK8CCRwVsyF9 x/ITM9PQR2u3skja64w/lrvOIJYYBB++/kzStKnNpSIcw+mKSh14yeWKz6i9qZ+5 IKdjQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=g9+BMs0XSRU+TEL5autFFTSfwgP9oriaX4KRr7T+l jM=; b=iT28tPM5hhzvG4p+J86ztkieOXCbaHOWPdR2jrZ0sDrQ5ZQXgYA+R++Ub 4qO/haRPKZRuxsxuxGqid/xvCeAA1lKVBKMmUvUkIjv4bqKdnnscPVbgzp8fYbx3 0F6k345iytYZMiemkw8jbLAYuvN9NsTy1Kw/EOkOJio369cu1Y8J4raDaxMqBxFh aQUyr3atNR//KvmeA8x2CMS5LC/NrzbUVlcDdGsqIXMN9FV+Q/XH3ozYIB+Meejv fzwNizL0szzBr5M7qEa9YJfmkm7YtNEgEqtiwjlYfwKLjsuxWQaVwJaq4d+pEa8+ Tl3m8ASPjuQxxRbouk8mjFOjEWbPg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrudejgedgudefgecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuggftrfgrthhtvghrnhepgeetveefuefgledtgfdtudeihefhfedvgfeivddu hfeiueetjeeugefhueehgfevnecuffhomhgrihhnpeifihhkihhpvgguihgrrdhorhhgne cukfhppeejhedrudejvddrhedrvdefudenucevlhhushhtvghrufhiiigvpedtnecurfgr rhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrd gtohhm X-ME-Proxy: Received: from [192.168.1.3] (75-172-5-231.tukw.qwest.net [75.172.5.231]) by mail.messagingengine.com (Postfix) with ESMTPA id 8AFA53280059; Thu, 18 Jun 2020 15:06:19 -0400 (EDT) Subject: Re: create batch script to import into postgres tables To: Pepe TD Vo , Christopher Browne Cc: Pgsql-admin , Pgsql-general 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> <1814718409.184531.1592498400894@mail.yahoo.com> From: Adrian Klaver Message-ID: <4d5841c4-9a6c-0e20-2c05-c21d3d3942df@aklaver.com> Date: Thu, 18 Jun 2020 12:06:18 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: <1814718409.184531.1592498400894@mail.yahoo.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 > 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 > > > -- Adrian Klaver adrian.klaver@aklaver.com