pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedRe: Copying data from a CSV file into a table dynamically
8+ messages / 5 participants
[nested] [flat]
* Re: Copying data from a CSV file into a table dynamically
@ 2018-08-14 13:10 pavan95 <pavan.postgresdba@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: pavan95 @ 2018-08-14 13:10 UTC (permalink / raw)
To: pgsql-general
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?
Any suggestions or modifications are most welcome.
Regards,
Pavan
--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Copying data from a CSV file into a table dynamically
@ 2018-08-14 13:20 Adrian Klaver <adrian.klaver@aklaver.com>
parent: pavan95 <pavan.postgresdba@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: Adrian Klaver @ 2018-08-14 13:20 UTC (permalink / raw)
To: pavan95 <pavan.postgresdba@gmail.com>; pgsql-general
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
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Copying data from a CSV file into a table dynamically
@ 2018-08-14 13:38 pavan95 <pavan.postgresdba@gmail.com>
parent: Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 2 replies; 8+ messages in thread
From: pavan95 @ 2018-08-14 13:38 UTC (permalink / raw)
To: pgsql-general
Hi Adrian,
I tried to use
*"COPY postgres_log1 FROM '/tmp/abc/xyz/postgresql-`date --date="0 days ago"
+%Y-%m-%d`_*.csv' WITH csv;"*
But it resulted in an error. How to issue such that it is understandable by
psql?
And I am completely unaware of python & psycopg2. Anything which suits my
requirement is enough!!
Thanks in Advance
Regards,
Pavan
--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Copying data from a CSV file into a table dynamically
@ 2018-08-14 13:46 Ron <ronljohnsonjr@gmail.com>
parent: pavan95 <pavan.postgresdba@gmail.com>
1 sibling, 2 replies; 8+ messages in thread
From: Ron @ 2018-08-14 13:46 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 08/14/2018 08:38 AM, pavan95 wrote:
> Hi Adrian,
>
> I tried to use
> *"COPY postgres_log1 FROM '/tmp/abc/xyz/postgresql-`date --date="0 days ago"
> +%Y-%m-%d`_*.csv' WITH csv;"*
>
> But it resulted in an error. How to issue such that it is understandable by
> psql?
>
> And I am completely unaware of python & psycopg2. Anything which suits my
> requirement is enough!!ostgresql-archive.org/PostgreSQL-general-f1843780.html
Why not:
cat /tmp/abc/xyz/postgresql-`date --date="0 days ago"+%Y-%m-%d`_*.csv' | \
psql YOURDB -c "COPY postgres_log1 FROM STDIN WITH csv;"
--
Angular momentum makes the world go 'round.
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Copying data from a CSV file into a table dynamically
@ 2018-08-14 14:01 Ruiqiang Chen <chenrq2005@gmail.com>
parent: Ron <ronljohnsonjr@gmail.com>
1 sibling, 1 reply; 8+ messages in thread
From: Ruiqiang Chen @ 2018-08-14 14:01 UTC (permalink / raw)
To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
CSV file has no limitation of # of rows, excel max row is 2^20. Am I
correct?
On Tue, Aug 14, 2018 at 9:46 AM, Ron <ronljohnsonjr@gmail.com> wrote:
> On 08/14/2018 08:38 AM, pavan95 wrote:
>
>> Hi Adrian,
>>
>> I tried to use
>> *"COPY postgres_log1 FROM '/tmp/abc/xyz/postgresql-`date --date="0 days
>> ago"
>> +%Y-%m-%d`_*.csv' WITH csv;"*
>>
>> But it resulted in an error. How to issue such that it is understandable
>> by
>> psql?
>>
>> And I am completely unaware of python & psycopg2. Anything which suits my
>> requirement is enough!!ostgresql-archive.org/
>> PostgreSQL-general-f1843780.html
>>
>
> Why not:
> cat /tmp/abc/xyz/postgresql-`date --date="0 days ago"+%Y-%m-%d`_*.csv' | \
> psql YOURDB -c "COPY postgres_log1 FROM STDIN WITH csv;"
>
> --
> Angular momentum makes the world go 'round.
>
>
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Copying data from a CSV file into a table dynamically
@ 2018-08-14 14:04 Adrian Klaver <adrian.klaver@aklaver.com>
parent: pavan95 <pavan.postgresdba@gmail.com>
1 sibling, 0 replies; 8+ messages in thread
From: Adrian Klaver @ 2018-08-14 14:04 UTC (permalink / raw)
To: pavan95 <pavan.postgresdba@gmail.com>; pgsql-general
On 08/14/2018 06:38 AM, pavan95 wrote:
> Hi Adrian,
>
> I tried to use
> *"COPY postgres_log1 FROM '/tmp/abc/xyz/postgresql-`date --date="0 days ago"
> +%Y-%m-%d`_*.csv' WITH csv;"*
>
> But it resulted in an error. How to issue such that it is understandable by
> psql?
>
> And I am completely unaware of python & psycopg2. Anything which suits my
> requirement is enough!!
Well I was just using that as an example. If you are not familiar with
Python then you would probably be better off using a language and it's
associated Postgres adapter you are comfortable with.
>
> Thanks in Advance
>
> Regards,
> Pavan
>
>
>
> --
> Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Copying data from a CSV file into a table dynamically
@ 2018-08-14 14:25 David G. Johnston <david.g.johnston@gmail.com>
parent: Ruiqiang Chen <chenrq2005@gmail.com>
0 siblings, 0 replies; 8+ messages in thread
From: David G. Johnston @ 2018-08-14 14:25 UTC (permalink / raw)
To: Ruiqiang Chen <chenrq2005@gmail.com>; +Cc: Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Tuesday, August 14, 2018, Ruiqiang Chen <chenrq2005@gmail.com> wrote:
> CSV file has no limitation of # of rows, excel max row is 2^20. Am I
> correct?
>
It's considered bad form to hijack threads in this manner.
Text files have no inherent limits. You can check the Excel documentation
for the version you care about to learn it's limits.
David J.
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Copying data from a CSV file into a table dynamically
@ 2018-08-16 12:58 pavan95 <pavan.postgresdba@gmail.com>
parent: Ron <ronljohnsonjr@gmail.com>
1 sibling, 0 replies; 8+ messages in thread
From: pavan95 @ 2018-08-16 12:58 UTC (permalink / raw)
To: pgsql-general
Thank you so much Ron. You saved my efforts after a slight modification of
quotes of the command you said, it started working fine.
The modified command is as below:
cat /tmp/xyz/abc/postgresql-`date --date="0 days ago" +%Y-%m-%d`_*.csv |
psql -U aaa -d mydb -c "COPY postgres_log1 FROM STDIN WITH csv;"
Team,
Thank you so much for your valuable time spent on this!!
Regards,
Pavan
--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html
^ permalink raw reply [nested|flat] 8+ messages in thread
end of thread, other threads:[~2018-08-16 12:58 UTC | newest]
Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-08-14 13:10 Re: Copying data from a CSV file into a table dynamically pavan95 <pavan.postgresdba@gmail.com>
2018-08-14 13:20 ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-08-14 13:38 ` pavan95 <pavan.postgresdba@gmail.com>
2018-08-14 13:46 ` Ron <ronljohnsonjr@gmail.com>
2018-08-14 14:01 ` Ruiqiang Chen <chenrq2005@gmail.com>
2018-08-14 14:25 ` David G. Johnston <david.g.johnston@gmail.com>
2018-08-16 12:58 ` pavan95 <pavan.postgresdba@gmail.com>
2018-08-14 14:04 ` Adrian Klaver <adrian.klaver@aklaver.com>
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