pgexport - a Perl utility to download ASCII files from PostgreSQL databases
This version is 0.9.1, released 2004-02-12
Enhancements in this version
option for XML export for Kugar reporting engine
Bugfixes in this version
fixed overwriting of randomly generated filename
previous versions:
0.9.0 (2004-01-30), initial release
command-line method:
pgexport mode dbhost dbport dbname dbuser dbpass tablename datafile mask_or_delimiter firstrecord specfile fieldlist forcecase quotemark apostrophes
where mode is d or f
or
command-line method, XML output:
pgexport mode dbhost dbport dbname dbuser dbpass tablename datafile implicit_level default_level template temporary forcecase
mode is always k
or
configuration file method:
pgexport c configfile
All arguments are mandatory, but some may be marked as NULL.
pgexport allows for the export of delimited or fixed-width ASCII text from PostgreSQL databases, and for XML output for use in the Kugar reporting engine.
I wrote this because (1) COPY is limited to the superuser (2) \copy is a crippled version of COPY (3) pgadmin3 and pgaccess have broken import/export functionality (at least on Fedora Core 1 - yes, with the updates installed via yum) (4) I am trying to extricate myself from certain proprietary software products from Redmond, Washington. A replacement for a certain desktop database is the last piece of the puzzle - and I am only too happy to share with others.
So, in the great tradition of Open-Source, I rolled my own.
Yes, there is a pgimport.
This software (pgexport) may be used under either the GNU General Public License, version 2 (or at your option, any later version), or the Artistic License.
No warranty or guarantee, either express or implied, exists for this software or for the use of this software. You use this software at your own risk.
pgexport was developed using Fedora Core 1 (updated via yum to 2004-01-26), PostgreSQL 7.3.4, and Perl 5.8.1 i386-linux-thread-multi.
command-line method
NOTE: When NULL is an option, the literal string ``NULL'' is meant, without any quotation marks. A ``0'' (zero) can also be substituted where NULL is used (again, no quotation marks).
If mode is d, then the field delimiter is entered here (use \t for tabs). There is NO default delimiter.
If mode is f, then a pack mask may be given here, or NULL passed. (See Perl documentation for functions pack()
and unpack()). Pack masks should use only the A template character for ASCII data (i.e. do not use a template character other than A unless you know what you are doing).
If mode if d, pass a 1 here to indicate that the datafile should be exported with field names in its first record. If it does not, pass NULL.
If no layout and specification file is used, pass NULL.
p preserves the case of all values and fieldnames. This is the default
u forces all values and fieldnames to uppercase.
l forces all values and fieldnames to lowercase.
quotemark should be NULL if mode is f.
If you are using pgexport in command-line mode and you want to use the single-quote or double-quote character, you must escape the character with a backslash. However, if you are using configuration file mode, do NOT escape the character with a backslash.
If apostrophes is 1, single-quotes and apostrophes will be preserved. This is the default.
If apostrophes is 0, single-quotes and apostrophes are eliminated from values before insertion, and are therefore lost.
If you want to use the single-quote character in quotemark, then take care that apostrophes is 0.
command-line method, XML output
Passing a NULL here will cause pgexport to generate a filename.
If you want pgexport to provide a default level, enter a 1.
If you do NOT want pgexport to provide a default level, but will provide it yourself, enter a 0.
The default is 1 (pgexport provides a default level). Enter NULL to obtain the default.
The default value for default_level is 0. Enter NULL to obtain the default.
Pass a 1 here to place the data file into /tmp. Pass a 0 here for a ``permanent'' file.
The default is 1.
p preserves the case of all values and fieldnames. This is the default
u forces all values and fieldnames to uppercase.
l forces all values and fieldnames to lowercase.
configuration file method
In the configuration file method, mode is c. Instead of reading options from the command line, they are read from configfile. The entries in configfile should appear in the same order, using the same syntax, as for the command-line method, except that a newline should terminate each argument (i.e. the arguments should form a list ``reading down'').
Array @config holds the information obtained from the command line or from the configuration file. The elements of the array are used as follows. Please see the appropriate entries in section OPTIONS.
$config[0]: mode
$config[1]: dbhost
$config[2]: dbport
$config[3]: dbname
$config[4]: dbuser
$config[5]: dbpass
$config[6]: tablename
$config[7]: datafile
$config[8]: mask_or_delimiter (implicit_level for mode k)
$config[9]: firstrecord (default_level for mode k)
$config[10]: specfile (template for mode k)
$config[11]: fieldlist (temporary for mode k)
$config[12]: forcecase
$config[13]: quotemark (not used for mode k)
$config[14]: apostrophes (not used for mode k)
Please see section OPTIONS for required fields, specifically datafile and specfile.
I am too lazy to go over every error message here. Besides, the ones you will get from STDOUT are descriptive enough.
However, some words of note and caution are in order.
1. All fields are exported as text. If you choose a quoting character, all fields will be quoted.
2. For delimited files, obtaining field names from the database table is NOT the default. The order is: specfile, fieldlist, database table. If you want to use the database table/view field names, make sure specfile and fieldlist are both NULL.
3. Using a specfile or fieldlist for delimited data is not a bad idea - pgexport checks to make sure the number of fields in the table or view matches the number of fields in your filespec or fieldlist. A field count is also always performed for fixed-width records.
4. If you have special data types (i.e. dates, currency) that need special formatting before export, handle that using a database view; pgexport does not perform any data formatting.
5. If a file exists with the same name as datafile, the program will terminate.
6. If you are using the program in command-line mode and want to use the double-quote or single-quote character for quoting, you must escape it first with a backslash. You do not escape characters in configuration file mode.
7. For those of you on Unixlike systems, you may want to run unix2dos after export before sending the file out into the WinDOS world.
=back
Perl 5.004 or higher, Text::ParseWords, DBI, DBD::Pg
Term::ReadKey is required for no-echo password entry from the command line when prompted. If this module is unavailable, the program will still run, but no-echo entry will not be available (you will have to enter your password in clear text from the command line). See previous entry for dbpass.
pack(), unpack(), Text::ParseWords, DBI, DBD:Pg, Term::ReadKey
Many thanks to the PostgreSQL development team for a database I can live with (meaning inlined functions using PL/Perl), to Larry Wall and his little helpers for the best utility language in the world, and to the contributors to Fedora Core 1.
Wayne Matthew Syvinski, matthew@techcelsior.com