Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fpZFm-0000VO-7Y for pgsql-general@arkaria.postgresql.org; Tue, 14 Aug 2018 13:20:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fpZFj-0000TA-My for pgsql-general@arkaria.postgresql.org; Tue, 14 Aug 2018 13:20:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fpZFK-0006xS-4S for pgsql-general@lists.postgresql.org; Tue, 14 Aug 2018 13:20:26 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fpZFA-00052Y-Sr for pgsql-general@postgresql.org; Tue, 14 Aug 2018 13:20:24 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id AEDF72AD; Tue, 14 Aug 2018 09:20:13 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 14 Aug 2018 09:20:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=IJ5rm/y8jJBw1GxKqP1ODrLkUCbZr jwPiDC/FqKY4eE=; b=zBfI1IEOUw5LHN0r8dTWe03EL7yIkdak+ph9iIdno4dSv 5t9cJoxq5mTQmrg7lvAqvvog5gty7vrKx4yiAA2to0JAgb0RmA0AGEnpr25PsvbV wdEqE+9sv/fz3/I5aIMRZa9S9b4urQN4UKKbGxVmEu6Jzxgcpu3q6K6WC9RT065A g38ZRwSJHCE7rbi7MTjAao+6MWhUWiXnkW7sWLvuNQ4yPBRNgtqfUdrJQllZLoE2 bElVcrehQ05FaAhvo5c67HiWGzzpnyqeYc/256PYGFkDyUeHsKZ+fcHiy1Sl14ok PSQyOK6GXXTevuvWao5EnSdlw850NoQJE/OGx9wmg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=IJ5rm/ y8jJBw1GxKqP1ODrLkUCbZrjwPiDC/FqKY4eE=; b=pjDAOU6zubHSx4pCmNUU5b zREIWIdXbokLzhC2h39sf1RDRS9SLq6Q85BG0/6P7H9NndG6NBmOtOPDNbXHyNo2 fqcYO9lfk09UUnAoKoL83QjG3nGYzxMSsGI3kT5ZfgdA6fHzRldTvDFmzoyZPDw8 Dom/V0BlUGO3tP+Qwz6M842WqWpB95GBTcLDAAZT2EjLOBc5SxGIPh7eLk/6qldG 60bBHUbIfbT+sEx0Gjz5EjCx0POOHs3rH10Cm4k14/Tvs0mlmn4eMyi2c46B9WYC +vjh2FvbcS+tGs+SDw5Iia7UlbcAa9oPSH0zpwVkCk5QZTrWjtELvY0W0J73iogQ == X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id 98888E403B; Tue, 14 Aug 2018 09:20:12 -0400 (EDT) Subject: Re: Copying data from a CSV file into a table dynamically To: pavan95 , pgsql-general@postgresql.org References: <1534252253597-0.post@n3.nabble.com> From: Adrian Klaver Message-ID: <318228ff-6861-8a69-981f-48cb528a0dd9@aklaver.com> Date: Tue, 14 Aug 2018 06:20:11 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <1534252253597-0.post@n3.nabble.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 08/14/2018 06:10 AM, pavan95 wrote: > Hi all, > > I am well versed with the COPY command for copying the contents of a csv > file into a table. I am used to the below mentioned command: > > * COPY postgres_log1 FROM '/tmp/abc/xyz/postgresql-2018-08-14_000000.csv' > WITH csv;* > > But in the location "/tmp/abc/xyz/" daily a file(with header > postgresql-2018-08-14_000000.csv) will be generated. > > So how can I automate this task instead of manually specifying the file > name, it has to look for the filename with current_date at the time of > copying into table. How can I achieve this? COPY FROM can do so from a program: https://www.postgresql.org/docs/10/static/sql-copy.html "PROGRAM A command to execute. In COPY FROM, the input is read from standard output of the command, and in COPY TO, the output is written to the standard input of the command. Note that the command is invoked by the shell, so if you need to pass any arguments to shell command that come from an untrusted source, you must be careful to strip or escape any special characters that might have a special meaning for the shell. For security reasons, it is best to use a fixed command string, or at least avoid passing any user input in it. " So maybe create a program that does the file look up and then sends the data to stdout for consumption by COPY. OR Just create a program/script that does it all, find the file and invoke COPY on the file. I do similar things using Python and psycopg2. > > Any suggestions or modifications are most welcome. > > Regards, > Pavan > > > > -- > Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html > > -- Adrian Klaver adrian.klaver@aklaver.com