From a6f39b267060858a3d650cc598220a07d0cdbb8f Mon Sep 17 00:00:00 2001
From: Martin Pitt <martin@piware.de>
Date: Thu, 19 May 2005 21:43:51 +0000
Subject: [PATCH] merge changes from laptop branch

---
 postgresql-common-9/TODO                 |  2 -
 postgresql-common-9/debian/README.Debian | 84 ++++++++++++++++++++++++
 postgresql-common-9/debian/changelog     |  8 ++-
 postgresql-common-9/pg_createcluster     |  6 ++
 postgresql-common-9/pg_ctlcluster        |  9 ++-
 postgresql-common-9/pg_wrapper           | 25 ++++++-
 6 files changed, 126 insertions(+), 8 deletions(-)
 create mode 100644 postgresql-common-9/debian/README.Debian

diff --git a/postgresql-common-9/TODO b/postgresql-common-9/TODO
index b5db4f8b..0c8b0a58 100644
--- a/postgresql-common-9/TODO
+++ b/postgresql-common-9/TODO
@@ -3,6 +3,4 @@ postgresql TODO
 
 - Provide do.maintenance which vacuums all running clusters.
 - Handle configuration files in pg_upgradecluster somehow.
-- Check validity of locale before initdb and pg_ctl start.
-- Handle stale pid files/sockets.
 - add CAN numbers to per-version changelogs(?)
diff --git a/postgresql-common-9/debian/README.Debian b/postgresql-common-9/debian/README.Debian
new file mode 100644
index 00000000..f07f4742
--- /dev/null
+++ b/postgresql-common-9/debian/README.Debian
@@ -0,0 +1,84 @@
+PostgreSQL for Debian
+=====================
+
+PostgreSQL is the successor to Postgres95, which in turn succeeded POSTGRES.
+PostgreSQL is a relational database with object-oriented extensions.  It
+implements the greater part of SQL-92 and is intended to implement it in full;
+in addition it supports its own extended facilities.
+
+Since the on-disk data format of all major PostgreSQL versions (like 7.2, 7.4,
+8.0, etc.) is incompatible to each other, Debian's PostgreSQL packaging
+architecture is designed to maintain clusters of different major versions in
+parallel.
+
+This postgresql-common package provides the common infrastructure and all
+frontend programs that users and administrators use. The version specific
+server and client programs are shipped in postgresql-*-<version> packages.
+
+For a detailled description of the architecture, please see
+
+  /usr/share/doc/postgresql-common/architecture.html
+
+First steps for the impatient
+-----------------------------
+Eventually you will not get around reading at least some parts of the manual,
+but if you want to get straight into playing SQL, here are the steps to create
+a database user and a database for the Unix user 'joe':
+
+1. Install a database server with the major version of your choice, preferrably
+   the latest version. This will automatically create a default cluster 'main'
+   with the database superuser 'postgres'.
+
+2. Get a shell for the database superuser 'postgres'. If your system has an
+   active root user, use su:
+
+   $ su -c "su postgres"
+
+   If your system uses sudo to get administrative rights, use sudo instead:
+
+   joe$ sudo -u postgres sh
+
+3. In this postgres shell, create a database user with the same name as your
+   Unix login:
+
+   $ createuser -A -D joe
+
+   For details about the options, see createuser(1).
+
+4. Create a database "joework" which is owned by "joe":
+
+   $ createdb -O joe joework
+
+   For details about the options, see createdb(1).
+
+5. Exit the postgres shell.
+
+6. As user joe, you should now be able to connect to your database with
+
+   $ psql joework
+
+
+Cluster management
+------------------
+For managing clusters, the following commands are provided (each with its own
+manual page):
+
+   pg_createcluster  - Create a new cluster or integrate an existing one into
+                       the postgresql-common architecture.
+   pg_dropcluster    - Completely remove a cluster.
+   pg_ctlcluster     - Control the server process of a cluster (start, stop,
+                       restart).
+   pg_lsclusters     - Show a list of all existing clusters and their status.
+   pg_upgradecluster - Migrate a cluster from one major version to another one.
+
+Further documentation
+---------------------
+All commands shipped by postgresql-common have detailled manpages. See
+postgresql-common(7) for the documentation of the database client program
+wrapping, and user_clusters(5) and postgresqlrc(5) for the cluster
+configuration.
+
+The documentation of the database server and client functions, SQL commands,
+modules, etc.  documented is shipped in the per-version packages
+postgresql-doc-<version>.
+
diff --git a/postgresql-common-9/debian/changelog b/postgresql-common-9/debian/changelog
index 0bf982b5..64372e7d 100644
--- a/postgresql-common-9/debian/changelog
+++ b/postgresql-common-9/debian/changelog
@@ -1,8 +1,12 @@
 postgresql-common (9) experimental; urgency=low
 
-  *
+  * Add README.Debian with some general introduction, "first steps for the
+    impatient", and pointers to further documentation.
+  * pg_ctlcluster: Check validity of postmaster locale before setting it.
+  * pg_createcluster: Check validity of locale before calling initdb under it.
+  * pg_wrapper: Support PGCLUSTER environment variable. Closes: #305912
 
- -- Martin Pitt <mpitt@debian.org>  Fri, 13 May 2005 00:51:18 +0200
+ -- Martin Pitt <mpitt@debian.org>  Thu, 19 May 2005 22:21:13 +0200
 
 postgresql-common (8) experimental; urgency=low
 
diff --git a/postgresql-common-9/pg_createcluster b/postgresql-common-9/pg_createcluster
index 43feadfd..6fbdf242 100755
--- a/postgresql-common-9/pg_createcluster
+++ b/postgresql-common-9/pg_createcluster
@@ -8,6 +8,7 @@
 use lib '/usr/share/postgresql-common';
 use PgCommon;
 use Getopt::Long;
+use POSIX qw/setlocale LC_ALL/;
 
 # call initdb 
 # Arguments: <version> <data directory> <owner uid> <owner gid>
@@ -106,6 +107,11 @@ sub add_local_hba_entry {
 
 exit 1 unless GetOptions ('u|user=s' => \$owneruid, 'g|group=s' => \$ownergid);
 
+# check validity of locale
+unless (setlocale (LC_ALL, "")) {
+    error ('The locale requested by the environment is invalid.')
+}
+
 if ($#ARGV < 1) {
     print "Usage: $0 [-u <uid>] [-g <gid>] <version> <cluster name> [<data directory>]\n";
     exit 1;
diff --git a/postgresql-common-9/pg_ctlcluster b/postgresql-common-9/pg_ctlcluster
index 250597ad..f8b566a6 100755
--- a/postgresql-common-9/pg_ctlcluster
+++ b/postgresql-common-9/pg_ctlcluster
@@ -8,7 +8,7 @@
 
 use lib '/usr/share/postgresql-common';
 use Getopt::Long;
-use POSIX qw/setsid/;
+use POSIX qw/setsid setlocale LC_ALL/;
 use PgCommon;
 
 # command line options
@@ -89,7 +89,12 @@ sub start {
     close CTRL;
     $locale or error ('Could not parse locale out of pg_controldata output');
 
-    # set locale used by initdb
+    # check validity of locale
+    unless (setlocale (LC_ALL, $locale)) {
+	error ("The server must be started under the locale $locale which does not exist any more.")
+    }
+
+    # set locale for the postmaster
     $ENV{'LC_ALL'} = $ENV{'LANG'} = $locale;
 
     @options = ($pg_ctl, 'start', '-D', $info{'pgdata'},'-l', $info{'logfile'}, '-s');
diff --git a/postgresql-common-9/pg_wrapper b/postgresql-common-9/pg_wrapper
index 7880d757..2b2ff62e 100755
--- a/postgresql-common-9/pg_wrapper
+++ b/postgresql-common-9/pg_wrapper
@@ -14,6 +14,14 @@ use PgCommon;
 $cmd = (split '/', $0)[-1];
 grep { $cmd eq $_ } @commands or die "pg_wrapper: invalid command name $cmd";
 
+# Check for PGCLUSTER in %ENV
+if (defined $ENV{PGCLUSTER}) {
+    ($version, $cluster) = split ('/', $ENV{PGCLUSTER}, 2);
+    error 'Invalid version specified with $PGCLUSTER' unless $version;
+    error 'Invalid cluster specified with $PGCLUSTER' unless $cluster;
+    error 'Cluster specified with $PGCLUSTER does not exist' unless cluster_exists $version, $cluster;
+}
+
 # Check for --cluster argument and filter it out
 for ($i = 0; $i <= $#ARGV; ++$i) {
     if ($ARGV[$i] eq '--cluster') {
@@ -66,9 +74,22 @@ configured cluster and database for the user and calls the appropriate version
 of the desired program to connect to that cluster and database, supplying any
 specifed options to that command.
 
-By default, the cluster is determined from the configuration files 
+By default, the cluster is determined from the configuration files
 L<user_clusters(5)> and L<postgresqlrc(5)>. However, this can be overriden by
-specifying the B<--cluster> I<version>/I<cluster> option.
+specifying the B<$PGCLUSTER> environment variable or the B<--cluster>
+I<version>/I<cluster> option.
+
+=head1 ENVIRONMENT
+
+=over
+
+=item B<PGCLUSTER>
+
+If B<$PGCLUSTER> is set, its value (of the form I<version>/I<cluster>)
+specifies the desired cluster, similar to the B<--cluster> option. However, if
+B<--cluster> is specified, it overrides the value of B<$PGCLUSTER>.
+
+=back
 
 =head1 FILES
 
-- 
GitLab