[postgis-users] postgis_restore.pl patch to allow psql options

strk at refractions.net strk at refractions.net
Wed May 18 10:17:00 PDT 2005


Getopt is ok to me, as far as no additional dependencies are introduced.

Actually the absence of any switch was not a design choice, just
the effect of extreme programming ;)

Using switches for almost all args would be helpful even in case we
decide to remove the need to specify the path to lwpostgis.sql.
This was initially done to allow a generic "filter" on dumps
(restore all but things defined in *this* sql file). That prerogative
can not be met anymore as we had to take care about skipping
"obsoleted" functions, which won't be found in new postgis.sql scripts.

So we could just define an associative array containing all
postgis objects definitions and completely drop parsing of an sql file
(as suggested by someone in the past). Having options specified
with switches will help making these kind of changes less traumatic.

Comments welcome.

--strk;

On Tue, May 17, 2005 at 02:31:35PM -0700, James Marca wrote:
> Hi, this is my third and final patch to postgis_restore.pl which
> allows one to pass psql options, such as username, host, and port.
> 
> It must be applied after the previous "addpod" patch, but I broke it
> out so that the maintainers can better decide what to keep and what to
> reject.
> 
> Finally, note that while I have used the final script on my machine, I
> only upgraded from postgres 8.0.1, postgis 1rc3 -> postgres 8.0.1,
> postgis 1.0.0, and I only did so on my Linux box. 
> 
> Hope this is useful.
> 
> James
>  
> --- postgis-1.0.0/utils/postgis_restore.pl.pod	2005-05-17 14:15:15.000000000 -0700
> +++ postgis-1.0.0/utils/postgis_restore.pl	2005-05-17 14:18:52.000000000 -0700
> @@ -38,12 +38,34 @@
>  #
>  use strict;
>  use warnings;
> +use Getopt::Long;
>  use Pod::Usage;
>  
>  # eval "exec perl -w $0 $@"
>  # 	if (0);
>  
> -(@ARGV == 3) || pod2usage(-exitstatus => 0, -verbose => 2);
> +# (@ARGV == 3) || pod2usage(-exitstatus => 0, -verbose => 2);
> +
> +# Grab options from the command line
> +my $help   = 0;
> +my $man    = 0;
> +my $dbuser   = getlogin();
> +my $dbhost="";
> +my $dbport="";
> +my $dbpassword=undef;
> +
> +my $p = new Getopt::Long::Parser;
> +$p->getoptions
> +    ( 'user|username|U=s'        => \$dbuser,
> +      'host|h=s'        => \$dbhost,
> +      'password|W'    => \$dbpassword, #does nothing, but lets people enter something
> +      'port|p=i'        => \$dbport,
> +      'help|?'        => \$help,  # see the pod (--help)
> +      'man|pod'         => \$man,
> +      ) or pod2usage(2);
> +
> +pod2usage(-verbose=>1) if $help;
> +pod2usage(-exitval => 0, -verbose => 2) if $man;
>  
>  my $DEBUG=1;
>  
> @@ -104,6 +126,12 @@ my %obsoleted_function = (
>  my $postgissql = $ARGV[0];
>  my $dbname = $ARGV[1];
>  my $dump = $ARGV[2];
> +
> +if ( ! $dump ) {
> +    pod2usage(-exitval => 2, -verbose => 0,
> +	      -msg => "Cannot proceed without a dumpfile to process.");
> +}
> +
>  my $dumplist=$dump.".list";
>  my $dumpascii=$dump.".ascii";
>  
> @@ -111,12 +139,29 @@ print "postgis.sql is $postgissql\n";
>  print "dbname is $dbname\n";
>  print "dumpfile is $dump\n";
>  
> -# form the various client program commands, for use later
> +# form the various client program commands, with appropriate options
>  
> -my $pg_restore_command= "pg_restore ";
> -my $createdb_command = "createdb  ";
> -my $createlang_command = "createlang   ";
> -my $psql_command = "psql  ";
> +my $pg_restore_command= "pg_restore -U $dbuser -W";
> +my $createdb_command = "createdb -U $dbuser -W  ";
> +my $createlang_command = "createlang -U $dbuser -W  ";
> +my $psql_command = "psql -U $dbuser -W  ";
> +
> +if($dbhost){
> +    foreach ($pg_restore_command,
> +	     $createdb_command,
> +	     $createlang_command,
> +	     $psql_command){
> +	$_ .=" -h $dbhost ";
> +    }
> +}
> +if($dbport){
> +    foreach ($pg_restore_command,
> +	     $createdb_command,
> +	     $createlang_command,
> +	     $psql_command){
> +	$_ .=" -p $dbport ";
> +    }
> +}
>  
>  #
>  # Scan postgis.sql
> @@ -675,6 +720,16 @@ Arguments:
>    db --- the new database name.  This cannot be an existing database
>    dump --- the dump file created by pg_dump
>  
> +Options:
> +
> + -h, --host=HOSTNAME  database server host or socket directory
> + -p, --port=PORT      database server port number
> + -U, --username=NAME  connect as specified database user
> + -W, --password       force password prompt (automatic, included 
> +                      only to mimic psql interface)
> + --help,?             brief help message
> + --man, --pod         full documentation
> +
>  =head1 ARGUMENTS
>  
>  =over 
> @@ -705,6 +760,50 @@ to get a dump file called olddb.dump
>  
>  =back
>  
> +=head1 OPTIONS
> +    
> +=over
> +
> +=item B<-h, --host>=HOSTNAME
> +
> +Specifies the host name of the machine on which the server is running.
> +If the value begins with a slash, it is used as the directory for the
> +Unix- domain socket. If omitted, the behavior will be identical to
> +that of psql without passing the -h option.
> +
> +=item B<-p, --port>=port 
> +
> +Specifies the TCP port or the local Unix-domain socket file extension
> +on which the server is lis- tening for connections. Defaults to the
> +value of the PGPORT environment variable or, if not set, to the port
> +specified at PostgreSQL compile time (usually 5432).  
> +
> +
> +=item B<-U, --username>=username  
> +
> +Connect to the database as the user username instead of the default.
> +(You must have permission to do so, of course.) If omitted, the
> +default current user will be obtained explicitly by a (Perl) call to
> +getlogin().
> +
> +=item B<-W, --password>
> +
> +This option isn't used.  In the future, if this script is extended to
> +use the Perl DBI, then this option will allow one to pass in the
> +correct password for the given username.  At the moment, all calls to
> +psql, pg_restore, etc explicitly include the '-W' parameter,
> +regardless of the contents of this command line option.
> +
> +=item B<--help>
> +
> +Print a brief help message and exits.
> +    
> +=item B<--man>, B<--pod>
> +    
> +Prints this manual page and exits.
> +    
> +=back
> +    
>  =head1 DESCRIPTION
>  
>  B<postgis_restore.pl> is aimed at restoring postgis data from a
> @@ -761,8 +860,8 @@ The original code was tested on the foll
>  
>  =back
>  
> -Note that this version, which adds pod, pod2usage calls, and modifies
> -some small pieces of logic, has not been tested on these platforms.
> +This version, which includes host, port, and username options, has not
> +been tested on these platforms.
>  
>  =head1 AUTHOR
>  
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users



More information about the postgis-users mailing list