473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can COPY skip columns?


Hi,

Is it possible for the COPY command to read data from a file, but skip
specific columns?

Thanks

Adam
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #1
16 28299
Adam Witney wrote:
Hi,

Is it possible for the COPY command to read data from a file, but skip
specific columns?
\h copy

COPY tablename [ ( column [, ...] ) ]
TO { 'filename' | STDOUT }
[ [ WITH ]
[ BINARY ]
[ OIDS ]
[ DELIMITER [ AS ] 'delimiter' ]
[ NULL [ AS ] 'null string' ]
[ CSV [ QUOTE [ AS ] 'quote' ]
[ ESCAPE [ AS ] 'escape' ]
[ FORCE QUOTE column [, ...] ]

Yes.. you just have to specify the columns...

Sincerely,

Joshua D. Drake


Thanks

Adam

--
Command Prompt, Inc., home of PostgreSQL Replication, and plPHP.
Postgresql support, programming shared hosting and dedicated hosting.
+1-503-667-4564 - jd@commandprompt.com - http://www.commandprompt.com
Mammoth PostgreSQL Replicator. Integrated Replication for PostgreSQL
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #2
Adam Witney <aw*****@sghms.ac.uk> writes:
Is it possible for the COPY command to read data from a file, but skip
specific columns?


Nope. When you get into significant massaging of the input data,
usually the best bet is to COPY into a temp table that exactly matches
the format of the data file, and then do your rearrangements using an
INSERT/SELECT into the final target table.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #3

Hi Joshua,

Sorry, I meant skip a column in the file, not the database table, or is that
what you meant?

Thanks

adam
Adam Witney wrote:
Hi,

Is it possible for the COPY command to read data from a file, but skip
specific columns?


\h copy

COPY tablename [ ( column [, ...] ) ]
TO { 'filename' | STDOUT }
[ [ WITH ]
[ BINARY ]
[ OIDS ]
[ DELIMITER [ AS ] 'delimiter' ]
[ NULL [ AS ] 'null string' ]
[ CSV [ QUOTE [ AS ] 'quote' ]
[ ESCAPE [ AS ] 'escape' ]
[ FORCE QUOTE column [, ...] ]

Yes.. you just have to specify the columns...

Sincerely,

Joshua D. Drake


Thanks

Adam

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #4
Joshua D. Drake wrote:
Adam Witney wrote:
Hi,

Is it possible for the COPY command to read data from a file, but skip
specific columns?

Crap, read your message backwards. Sorry. No you can't do this.

Sincerely,

Joshua D. Drake


\h copy

COPY tablename [ ( column [, ...] ) ]
TO { 'filename' | STDOUT }
[ [ WITH ]
[ BINARY ]
[ OIDS ]
[ DELIMITER [ AS ] 'delimiter' ]
[ NULL [ AS ] 'null string' ]
[ CSV [ QUOTE [ AS ] 'quote' ]
[ ESCAPE [ AS ] 'escape' ]
[ FORCE QUOTE column [, ...] ]

Yes.. you just have to specify the columns...

Sincerely,

Joshua D. Drake


Thanks

Adam


------------------------------------------------------------------------
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

--
Command Prompt, Inc., home of PostgreSQL Replication, and plPHP.
Postgresql support, programming shared hosting and dedicated hosting.
+1-503-667-4564 - jd@commandprompt.com - http://www.commandprompt.com
Mammoth PostgreSQL Replicator. Integrated Replication for PostgreSQL
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #5
Adam Witney wrote:
Hi Joshua,

Sorry, I meant skip a column in the file, not the database table, or is that
what you meant?
No, I read your question backwards. I am sorry. As Tom Lane said, copy
the file into a temporary table and then you can deal with it from there.

Sincerely,
Joshua D. Drake

Thanks

adam

Adam Witney wrote:
Hi,

Is it possible for the COPY command to read data from a file, but skip
specific columns?


\h copy

COPY tablename [ ( column [, ...] ) ]
TO { 'filename' | STDOUT }
[ [ WITH ]
[ BINARY ]
[ OIDS ]
[ DELIMITER [ AS ] 'delimiter' ]
[ NULL [ AS ] 'null string' ]
[ CSV [ QUOTE [ AS ] 'quote' ]
[ ESCAPE [ AS ] 'escape' ]
[ FORCE QUOTE column [, ...] ]

Yes.. you just have to specify the columns...

Sincerely,

Joshua D. Drake
Thanks

Adam


--
Command Prompt, Inc., home of PostgreSQL Replication, and plPHP.
Postgresql support, programming shared hosting and dedicated hosting.
+1-503-667-4564 - jd@commandprompt.com - http://www.commandprompt.com
Mammoth PostgreSQL Replicator. Integrated Replication for PostgreSQL
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #6
On 18/11/04 5:15 pm, "Tom Lane" <tg*@sss.pgh.pa.us> wrote:
Adam Witney <aw*****@sghms.ac.uk> writes:
Is it possible for the COPY command to read data from a file, but skip
specific columns?


Nope. When you get into significant massaging of the input data,
usually the best bet is to COPY into a temp table that exactly matches
the format of the data file, and then do your rearrangements using an
INSERT/SELECT into the final target table.


Ah ok. Can COPY be made to skip the first row then? To avoid the column
headers?

Thanks

adam
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #7

On Nov 18, 2004, at 11:53 AM, Adam Witney wrote:

Hi,

Is it possible for the COPY command to read data from a file, but skip
specific columns?


You can use awk to skip fields and create an intermediate file or
better yet, just pipe the output to copy. Here is a trivial example:

awk '{ FS = "\t" ; OFS = "\t" ; print $1,$3 }' inputdatafile

This sets the input and output field separators to tab and outputs the
first and third fields from inputdatafile. If you want to skip the
first record just add the following if statement:

awk '{ FS = "\t" ; OFS = "\t" ; if ( NR > 1 ) print $1,$3 }'
inputdatafile


Patrick B. Kelly
------------------------------------------------------
http://patrickbkelly.org
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #8
Another thing you can do if you know how to use Perl is to write a load
procedure for the ascii file. We are dumping data from a Progress database so
there was quite a lot of massaging to do, especially with some tables. We
read the Progress dump in and write it back out in the format we need for
PostgreSQL. Then we use COPY. Perl is excellent for this typ of work.

On Thursday 18 November 2004 12:15 pm, Tom Lane saith:
Adam Witney <aw*****@sghms.ac.uk> writes:
Is it possible for the COPY command to read data from a file, but skip
specific columns?


Nope. When you get into significant massaging of the input data,
usually the best bet is to COPY into a temp table that exactly matches
the format of the data file, and then do your rearrangements using an
INSERT/SELECT into the final target table.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


--

Work: 1-336-372-6812
Cell: 1-336-363-4719
email: te***@esc1.com

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #9

Following on from my question yesterday... Can COPY then be made to skip a
header line (the first line of the file say)?

The problem is this... I need to allow a user to upload a data file through
a web browser (PHP driven). This is then processed and the selected file
columns mapped to fields in a database, and then the data file uploaded. I
can do this fine with small files.... But if I get above a 1000 rows it
takes so long it time out.

I can upload a 10,000 row equivalent file using COPY from psql in 2 seconds,
so the time is down to the PHP processing (really all it does is send itto
pg_put_Line)

I liked Toms idea to create a temporary table, but I need to be able to get
rid of the header row then... Is there anyway of avoiding getting PHP
involved in the file processing?

Any ideas?

Thanks

Adam
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #10
Wouldn't it be easier to have PHP remove the first row?

And PHP has to be there because Apache is sending all the data to it.

Hope this helps,

On Fri, Nov 19, 2004 at 03:43:18PM +0000, Adam Witney wrote:

Following on from my question yesterday... Can COPY then be made to skip a
header line (the first line of the file say)?

The problem is this... I need to allow a user to upload a data file through
a web browser (PHP driven). This is then processed and the selected file
columns mapped to fields in a database, and then the data file uploaded. I
can do this fine with small files.... But if I get above a 1000 rows it
takes so long it time out.

I can upload a 10,000 row equivalent file using COPY from psql in 2 seconds,
so the time is down to the PHP processing (really all it does is send itto
pg_put_Line)

I liked Toms idea to create a temporary table, but I need to be able to get
rid of the header row then... Is there anyway of avoiding getting PHP
involved in the file processing?

Any ideas?

Thanks

Adam


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org
--
Martijn van Oosterhout <kl*****@svana.org> http://svana.org/kleptog/ Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFBnhlmY5Twig3Ge+YRAnsWAKCBEN8tsJdalrkYYXlFCf duSYFbmQCgw+35
ID9bZBkVZ7+5pxR8zuRNGlg=
=xW5J
-----END PGP SIGNATURE-----

Nov 23 '05 #11

Hi Martijn,

Can I get PHP to remove the first row without reading the whole file in? If
there was a way where PHP would just chop the first row off that would be
ideal...

Thanks

adam
Wouldn't it be easier to have PHP remove the first row?

And PHP has to be there because Apache is sending all the data to it.

Hope this helps,

On Fri, Nov 19, 2004 at 03:43:18PM +0000, Adam Witney wrote:

Following on from my question yesterday... Can COPY then be made to skip a
header line (the first line of the file say)?

The problem is this... I need to allow a user to upload a data file through
a web browser (PHP driven). This is then processed and the selected file
columns mapped to fields in a database, and then the data file uploaded. I
can do this fine with small files.... But if I get above a 1000 rows it
takes so long it time out.

I can upload a 10,000 row equivalent file using COPY from psql in 2 seconds,
so the time is down to the PHP processing (really all it does is send itto
pg_put_Line)

I liked Toms idea to create a temporary table, but I need to be able to get
rid of the header row then... Is there anyway of avoiding getting PHP
involved in the file processing?

Any ideas?

Thanks

Adam
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #12
> pg_put_Line)

I liked Toms idea to create a temporary table, but I need to be able to get
rid of the header row then... Is there anyway of avoiding getting PHP
involved in the file processing?
You could write a server side function to do the processing. You could
even use plPHP if that is your preferred language.

Sincerely,

Joshua D. Drake

Any ideas?

Thanks

Adam

--
Command Prompt, Inc., home of PostgreSQL Replication, and plPHP.
Postgresql support, programming shared hosting and dedicated hosting.
+1-503-667-4564 - jd@commandprompt.com - http://www.commandprompt.com
Mammoth PostgreSQL Replicator. Integrated Replication for PostgreSQL
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #13
Hi,
Can I get PHP to remove the first row without reading the whole file in? If
there was a way where PHP would just chop the first row off that would be
ideal...


If you are using "COPY table FROM stdin" and pg_put_line() it's not much
difficult to skip the first line ;)
Best regards
--
Matteo Beccati
http://phpadsnew.com
http://phppgads.com
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #14
can do this fine with small files.... But if I get above a 1000 rows it
takes so long it time out.
PHP is slow, but not *that* slow, you have a problem somewhere !
I can upload a 10,000 row equivalent file using COPY from psql in 2
seconds,
so the time is down to the PHP processing (really all it does is send
itto
pg_put_Line)


If you read the whole file in memory, the server will kick your script (I
think the default limit is 8 megabytes or something)...

So, I'd advise reading the file line by line using fgets() (dunno how it
is spelled in php), and just skip the first line, and pg_put_line() the
rest. This way you just use memory for one line at a time. ALso you can
echo (and flush) messages like 'XX lines inserted...' to the user while it
crunches.

If you're really stuck, and have command execution privileges, why not
system() a command line like "awk -blah your file | psql copy to your
table", or even launch it as a background process ?

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #15
MS SQL Server has a BULK INSERT command that can take a parameter "Start
Row" so that you can skip as many Rows as you want.
this is also available using the command line bcp utility. This would be
a nice feature for Postgresql to have , and I can't see it on any of the
to do lists, unless it is already implemented well enough in third party
utility or contrib. I shelled out a fair bit to get hold of EMS Hi Tech
Postgres Manager bundle, and whilst there is a lot to like about it
generally, at least part of the reason was because of the improved bulk
insert tools.

Adam Witney wrote:
Hi Martijn,

Can I get PHP to remove the first row without reading the whole file in? If
there was a way where PHP would just chop the first row off that would be
ideal...

Thanks

adam
Wouldn't it be easier to have PHP remove the first row?

And PHP has to be there because Apache is sending all the data to it.

Hope this helps,

On Fri, Nov 19, 2004 at 03:43:18PM +0000, Adam Witney wrote:

Following on from my question yesterday... Can COPY then be made to skip a
header line (the first line of the file say)?

The problem is this... I need to allow a user to upload a data file through
a web browser (PHP driven). This is then processed and the selected file
columns mapped to fields in a database, and then the data file uploaded. I
can do this fine with small files.... But if I get above a 1000 rows it
takes so long it time out.

I can upload a 10,000 row equivalent file using COPY from psql in 2 seconds,
so the time is down to the PHP processing (really all it does is send itto
pg_put_Line)

I liked Toms idea to create a temporary table, but I need to be able to get
rid of the header row then... Is there anyway of avoiding getting PHP
involved in the file processing?

Any ideas?

Thanks

Adam
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org



---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #16
On 19/11/04 6:46 pm, "Pierre-Frédéric Caillaud"
<li***@boutiquenumerique.com> wrote:
can do this fine with small files.... But if I get above a 1000 rows it
takes so long it time out.


PHP is slow, but not *that* slow, you have a problem somewhere !


Aha yes, I was reading the file doing this

fgets($fh, 1048576)))

.... But changing it to

fgets($fh, 1024)))

Which is a large enough amount of data for my needs improved the speed
dramatically!

Thanks to all those that replied

adam
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David Londeck | last post by:
I am using Visual Basic 2003 and I have written a notepad like application using the RichTextBox control. I am having trouble trying to emulate Microsoft Words text block copy/paste feature. In...
3
by: laurenq uantrell | last post by:
I want to copy all columns from tblA into tblB where tblA.UniqueID = parameter @ID without having to list each column name in an insert statement. Is this possible? Thanks, lq
0
by: Angelos | last post by:
Hello everybody... I need to do something simple (I believe)... I had a database with a products table.. and now I want to move everything in a new database wich the Table products has a different...
2
by: Peter Arrenbrecht | last post by:
Is it correct that "INSERT INTO new SELECT * FROM old" does not copy BLOB columns? Or is there something I failed to configure properly? Thank you, Peter Arrenbrecht Opus Software AG
2
by: Dennis | last post by:
I have a class which inherits from DataGrid and it is bound to a a DataTable. I have defined the GridColumnStyles I want to display. Also, I have added an "InsertCol" to my class whidh has the...
2
by: Marc Filthaut | last post by:
Hi, I have a problem, I have a table with a text type column and a nvarchar(2000) type column on my MS SQL 2000 Server. I know that the longest text in the text field is 1000 chars. I want to...
1
by: jmarr02s | last post by:
I am trying to "Copy all columns from another table" here is the SQL code I am using in MS Access: CREATE TABLE suppliers AS (SELECT * FROM companies WHERE id 1000); I think I got it to...
2
by: Soulless | last post by:
Hi, Is there a quick way to take all the rows from a column in a datagrid and copy them to a multi-line text box? I am hoping there is a quick method as opposed to looping through each row. ...
0
by: Taxman | last post by:
Windows XP, MS Office Excel 2003 If the tasks, I’m trying accomplish have been addressed previously (separately or in combination). Please, provide the links or keyword search to find them. I’ve...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.