X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id BE077D1C4BF for ; Wed, 13 Aug 2003 21:59:34 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 76274-04 for ; Wed, 13 Aug 2003 18:57:59 -0300 (ADT) Received: from loki.globexplorer.com (unknown [208.35.14.10]) by svr1.postgresql.org (Postfix) with ESMTP id 60E2CD1B56B for ; Wed, 13 Aug 2003 18:59:23 -0300 (ADT) X-MIMEOLE: Produced By Microsoft Exchange V6.0.6249.0 content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Subject: Re: importing db as text files Date: Wed, 13 Aug 2003 14:59:29 -0700 Message-ID: <71E37EF6B7DCC1499CEA0316A2568328DC9819@loki.globexplorer.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [GENERAL] importing db as text files Thread-Index: AcNh4yT3f1UsT74NRGuELPWpk9rCbwAA78ag From: "Gregory S. Williamson" To: "expect" , X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/652 X-Sequence-Number: 47069 I tend to use perl to preprocess dumps (in our case from Informix). It tend= s to work much faster than shell scripts (although your mileage may vary). = I have to fix missing numeric values (if the column allows nulls why can't = the copy command accept an empty field, I wonder?), missing dates. In other= cases is massages date formats, spatial data, etc. For example, a crude pr= ogram below to clean DOQQ metadata: Greg W. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D firenze% more infxunl2psql #!/usr/dist/bin/perl -w $FILE =3D $ARGV[0]; $OUTPUT =3D $ARGV[1]; $MODE =3D $ARGV[2]; open (INFILE,"$FILE"); open (OUTFILE,">$OUTPUT"); foreach $line () { chop($line); chop($line); if (($MODE cmp "DOQ") =3D=3D 0) { ($t_source_filename, $t_quadrangle_name, $t_west_longitude, $t_east= _longitude, $t_north_latitude, $t_south_latitude, $t_production_date, $t_ra= ster_order, $t_band_organization, $t_band_content, $t_bits_per_pixel, $t_sa= mples_and_lines, $t_horizontal_datum, $t_horizontal_coordinate_system, $t_c= oordinate_zone, $t_horizontal_units, $t_horizontal_resolution, $t_secondary= _horizontal_datum, $t_xy_origin, $t_secondary_xy_origin, $t_nw_quad_corner_= xy, $t_ne_quad_corner_xy, $t_se_quad_corner_xy, $t_sw_quad_corner_xy, $t_se= condary_nw_quad_xy, $t_secondary_ne_quad_xy, $tsecondary_se_quad_xy, $t_sec= ondary_sw_quad_xy, $t_rmse_xy, $t_image_source, $t_source_dem_date, $t_agen= cy, $t_producer, $t_production_system, $t_standard_version, $t_metadata_dat= e, $t_data_file_size, $byte_count) =3D split(/\|/,$line); if (length($t_production_date) =3D=3D 0) { $t_production_date =3D "\\N"; # psql seems to dump a blank = data with this nomenclature } if (length($t_coordinate_zone) =3D=3D 0) { # an integer $t_coordinate_zone =3D 0; } if (length($t_band_content) =3D=3D 0) { $t_band_content =3D 0; # also an int } if (length($t_bits_per_pixel) =3D=3D 0) { $t_bits_per_pixel =3D 0; # reasonable default for an int ? } if (length($t_horizontal_resolution) =3D=3D 0) { $t_horizontal_resolution =3D 0.0; } if (length($t_secondary_horizontal_datum) =3D=3D 0) { $t_secondary_horizontal_datum =3D "\'\'"; } if (length($t_rmse_xy) =3D=3D 0) { $t_rmse_xy =3D 0.0; } if (length($t_metadata_date) =3D=3D 0) { $t_metadata_date =3D "\\N"; } if (length($t_data_file_size) =3D=3D 0) { $t_data_file_size =3D 0; # a big int } if (length($byte_count) =3D=3D 0) { $byte_count =3D 0; # reasonable default ? for an int } $out_line =3D $t_source_filename . "|" . $t_quadrangle_name . "|" .= $t_west_longitude . "|" . $t_east_longitude . "|" . $t_north_latitude . "|= " . $t_south_latitude . "|" . $t_production_date . "|" . $t_raster_order . = "|" . $t_band_org anization . "|" . $t_band_content . "|" . $t_bits_per_pixel . "|" . $t_samp= les_and_lines . "|" . $t_horizontal_datum . "|" . $t_horizontal_coordinate_= system . "|" . $t_coordinate_zone . "|" . $t_horizontal_units . "|" . $t_ho= rizontal_resolu tion . "|" . $t_secondary_horizontal_datum . "|" . $t_xy_origin . "|" . $t_= secondary_xy_origin . "|" . $t_nw_quad_corner_xy . "|" . $t_ne_quad_corner= _xy . "|" . $t_se_quad_corner_xy . "|" . $t_sw_quad_corner_xy . "|" . $t_se= condary_nw_quad_xy . "|" . $t_secondary_ne_quad_xy . "|" . $tsecondary_se_q= uad_xy . "|" . $t_secondary_sw_quad_xy . "|" . $t_rmse_xy . "|" . $t_image_= source . "|" . $t_source_dem_date . "|" . $t_agency . "|" . $t_producer . "= |" . $t_production_system . "|" . $t_standard_version . "|" . $t_metadata_d= ate . "|" . $t_data_file_size . "|" . $byte_count; print OUTFILE "$out_line\n"; } else { print OUTFILE "$line\n"; } } close INFILE; close OUTFILE; -----Original Message----- From: Jason Godden [mailto:jasongodden@optushome.com.au] Sent: Wednesday, August 13, 2003 2:35 PM To: expect; pgsql-general@postgresql.org Subject: Re: [GENERAL] importing db as text files Hi expect, Best way in my opinion is to use the copy table command. This way Pg will= =20 actually 'massage' the data (string escapes and all) for you. If you have complex files best to use a combo of sed/awk to pre-massage the= =20 field delimiters and import the data to a temporary table. In most instanc= es=20 it is best to pipe the data to the psql command using copy table from stdin= =20 as from file requires that you are the process owner. Always use a temporary import table and perform validation/further conversi= on=20 in that table (IMO). eg (from one of my earlier posts) cat unprocesseddata.txt | sed "s/\",\"/$TAB/g" | sed "s/\"//g" | sed "s/.$/= /g"=20 | psql -dmydatabase -c "copy importtable from stdin delimiter '\t';" The first sed will replace all instances of "," with a tab character (I achieve tab in my shell scripts with a line like TAB=3D$(printf "\t")) The second sed will remove the start and end " because the first only check= s=20 for ",". The third sed will remove the Windows carriage returns (if any - depends on= =20 the platform the file was generated from. Note here that my file format never contains any " in a field so I can safe= ly=20 run the second sed. If your import file contains these then you will need = to=20 change the regex or use awk. Regards, Jason On Thu, 14 Aug 2003 07:14 am, expect wrote: > What's the big deal with importing text files? I have a 70 MB file to > import and it's been one problem after another. I used the copy command > and it appears that it's just not possible. I finally massaged the file > into a .sql file and ran that using \i db.sql but that failed too because= I > overlooked ' in names like D'Adario. The other problem I encountered was > that a numeric field had to have data in it, pg would not default to the > default value. So instead of massaging all the data again I decided to > change the data type for that column. This is my first experience with > postgresql and I'm wondering if I should expect to encounter similar pain > as I go further into this? So far it's been very painful trying to do wh= at > I thought would be easy and what I think should be easy. > > PostgreSQL 7.3.4 on linux redhat 9 > > > ---------------------------(end of broadcast)--------------------------- > TIP 7: don't forget to increase your free space map settings ---------------------------(end of broadcast)--------------------------- TIP 4: Don't 'kill -9' the postmaster