mirror of
https://github.com/postgres/postgres.git
synced 2025-09-02 04:21:28 +03:00
Cause initdb to create a third standard database "postgres", which
unlike template0 and template1 does not have any special status in terms of backend functionality. However, all external utilities such as createuser and createdb now connect to "postgres" instead of template1, and the documentation is changed to encourage people to use "postgres" instead of template1 as a play area. This should fix some longstanding gotchas involving unexpected propagation of database objects by createdb (when you used template1 without understanding the implications), as well as ameliorating the problem that CREATE DATABASE is unhappy if anyone else is connected to template1. Patch by Dave Page, minor editing by Tom Lane. All per recent pghackers discussions.
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.159 2005/06/06 20:22:57 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.160 2005/06/21 04:02:31 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -93,7 +93,7 @@ createdb(const CreatedbStmt *stmt)
|
||||
DefElem *dencoding = NULL;
|
||||
char *dbname = stmt->dbname;
|
||||
char *dbowner = NULL;
|
||||
char *dbtemplate = NULL;
|
||||
const char *dbtemplate = NULL;
|
||||
int encoding = -1;
|
||||
|
||||
#ifndef WIN32
|
||||
|
@@ -8,14 +8,19 @@
|
||||
*
|
||||
* To create the database cluster, we create the directory that contains
|
||||
* all its data, create the files that hold the global tables, create
|
||||
* a few other control files for it, and create two databases: the
|
||||
* template0 and template1 databases.
|
||||
* a few other control files for it, and create three databases: the
|
||||
* template databases "template0" and "template1", and a default user
|
||||
* database "postgres".
|
||||
*
|
||||
* The template databases are ordinary PostgreSQL databases. template0
|
||||
* is never supposed to change after initdb, whereas template1 can be
|
||||
* changed to add site-local standard data. Either one can be copied
|
||||
* to produce a new database.
|
||||
*
|
||||
* For largely-historical reasons, the template1 database is the one built
|
||||
* by the basic bootstrap process. After it is complete, template0 and
|
||||
* the default database, postgres, are made just by copying template1.
|
||||
*
|
||||
* To create template1, we run the postgres (backend) program in bootstrap
|
||||
* mode and feed it data from the postgres.bki library file. After this
|
||||
* initial bootstrap phase, some additional stuff is created by normal
|
||||
@@ -23,12 +28,10 @@
|
||||
* just embedded into this program (yeah, it's ugly), but larger chunks
|
||||
* are taken from script files.
|
||||
*
|
||||
* template0 is made just by copying the completed template1.
|
||||
*
|
||||
* Note:
|
||||
* The program has some memory leakage - it isn't worth cleaning it up.
|
||||
*
|
||||
*
|
||||
* This is a C implementation of the previous shell script for setting up a
|
||||
* PostgreSQL cluster location, and should be highly compatible with it.
|
||||
* author of C translation: Andrew Dunstan mailto:andrew@dunslane.net
|
||||
@@ -39,7 +42,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
* Portions taken from FreeBSD.
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.84 2005/06/17 22:32:47 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.85 2005/06/21 04:02:32 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -178,6 +181,7 @@ static void set_info_version(void);
|
||||
static void setup_schema(void);
|
||||
static void vacuum_db(void);
|
||||
static void make_template0(void);
|
||||
static void make_postgres(void);
|
||||
static void trapsig(int signum);
|
||||
static void check_ok(void);
|
||||
static char *escape_quotes(const char *src);
|
||||
@@ -1846,7 +1850,7 @@ make_template0(void)
|
||||
* We use the OID of template0 to determine lastsysoid
|
||||
*/
|
||||
"UPDATE pg_database SET datlastsysoid = "
|
||||
" (SELECT oid::int4 - 1 FROM pg_database "
|
||||
" (SELECT oid FROM pg_database "
|
||||
" WHERE datname = 'template0');\n",
|
||||
|
||||
/*
|
||||
@@ -1882,6 +1886,37 @@ make_template0(void)
|
||||
check_ok();
|
||||
}
|
||||
|
||||
/*
|
||||
* copy template1 to postgres
|
||||
*/
|
||||
static void
|
||||
make_postgres(void)
|
||||
{
|
||||
PG_CMD_DECL;
|
||||
char **line;
|
||||
static char *postgres_setup[] = {
|
||||
"CREATE DATABASE postgres;\n",
|
||||
NULL
|
||||
};
|
||||
|
||||
fputs(_("copying template1 to postgres ... "), stdout);
|
||||
fflush(stdout);
|
||||
|
||||
snprintf(cmd, sizeof(cmd),
|
||||
"\"%s\" %s template1 >%s",
|
||||
backend_exec, backend_options,
|
||||
DEVNULL);
|
||||
|
||||
PG_CMD_OPEN;
|
||||
|
||||
for (line = postgres_setup; *line; line++)
|
||||
PG_CMD_PUTS(*line);
|
||||
|
||||
PG_CMD_CLOSE;
|
||||
|
||||
check_ok();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* signal handler in case we are interrupted.
|
||||
@@ -2609,6 +2644,8 @@ main(int argc, char *argv[])
|
||||
|
||||
make_template0();
|
||||
|
||||
make_postgres();
|
||||
|
||||
if (authwarning != NULL)
|
||||
fprintf(stderr, "%s", authwarning);
|
||||
|
||||
|
@@ -4,7 +4,7 @@
|
||||
*
|
||||
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.57 2005/05/04 22:35:15 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.58 2005/06/21 04:02:32 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -442,7 +442,7 @@ test_postmaster_connection(void)
|
||||
for (i = 0; i < wait_seconds; i++)
|
||||
{
|
||||
if ((conn = PQsetdbLogin(NULL, portstr, NULL, NULL,
|
||||
"template1", NULL, NULL)) != NULL &&
|
||||
"postgres", NULL, NULL)) != NULL &&
|
||||
(PQstatus(conn) == CONNECTION_OK ||
|
||||
(strcmp(PQerrorMessage(conn),
|
||||
PQnoPasswordSupplied) == 0)))
|
||||
|
@@ -6,7 +6,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.59 2005/04/18 23:47:52 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.60 2005/06/21 04:02:32 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -297,13 +297,13 @@ main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
|
||||
conn = connectDatabase("template1", pghost, pgport, pguser, force_password);
|
||||
conn = connectDatabase("postgres", pghost, pgport, pguser, force_password);
|
||||
|
||||
printf("--\n-- PostgreSQL database cluster dump\n--\n\n");
|
||||
if (verbose)
|
||||
dumpTimestamp("Started on");
|
||||
|
||||
printf("\\connect \"template1\"\n\n");
|
||||
printf("\\connect \"postgres\"\n\n");
|
||||
|
||||
if (!data_only)
|
||||
{
|
||||
@@ -880,7 +880,7 @@ runPgDump(const char *dbname)
|
||||
/*
|
||||
* Win32 has to use double-quotes for args, rather than single quotes.
|
||||
* Strangely enough, this is the only place we pass a database name on
|
||||
* the command line, except template1 that doesn't need quoting.
|
||||
* the command line, except "postgres" which doesn't need quoting.
|
||||
*/
|
||||
#ifndef WIN32
|
||||
appendPQExpBuffer(cmd, "%s\"%s\" %s -Fp '", SYSTEMQUOTE, pg_dump_bin,
|
||||
|
@@ -3,7 +3,7 @@
|
||||
*
|
||||
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.117 2005/06/14 02:57:41 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.118 2005/06/21 04:02:33 tgl Exp $
|
||||
*/
|
||||
#include "postgres_fe.h"
|
||||
|
||||
@@ -195,7 +195,7 @@ main(int argc, char *argv[])
|
||||
{
|
||||
need_pass = false;
|
||||
pset.db = PQsetdbLogin(options.host, options.port, NULL, NULL,
|
||||
options.action == ACT_LIST_DB ? "template1" : options.dbname,
|
||||
options.action == ACT_LIST_DB ? "postgres" : options.dbname,
|
||||
username, password);
|
||||
|
||||
if (PQstatus(pset.db) == CONNECTION_BAD &&
|
||||
|
@@ -4,7 +4,7 @@
|
||||
*
|
||||
* Portions Copyright (c) 2002-2005, PostgreSQL Global Development Group
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/bin/scripts/clusterdb.c,v 1.12 2005/01/01 05:43:08 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/bin/scripts/clusterdb.c,v 1.13 2005/06/21 04:02:33 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -207,7 +207,7 @@ cluster_all_databases(const char *host, const char *port,
|
||||
PGresult *result;
|
||||
int i;
|
||||
|
||||
conn = connectDatabase("template1", host, port, username, password, progname);
|
||||
conn = connectDatabase("postgres", host, port, username, password, progname);
|
||||
result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn;", progname, echo);
|
||||
PQfinish(conn);
|
||||
|
||||
|
@@ -5,7 +5,7 @@
|
||||
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/bin/scripts/createdb.c,v 1.14 2004/12/31 22:03:17 pgsql Exp $
|
||||
* $PostgreSQL: pgsql/src/bin/scripts/createdb.c,v 1.15 2005/06/21 04:02:33 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -157,7 +157,8 @@ main(int argc, char *argv[])
|
||||
appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
|
||||
appendPQExpBuffer(&sql, ";\n");
|
||||
|
||||
conn = connectDatabase("template1", host, port, username, password, progname);
|
||||
conn = connectDatabase(strcmp(dbname, "postgres") == 0 ? "template1" : "postgres",
|
||||
host, port, username, password, progname);
|
||||
|
||||
if (echo)
|
||||
printf("%s", sql.data);
|
||||
|
@@ -5,7 +5,7 @@
|
||||
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.16 2004/12/31 22:03:17 pgsql Exp $
|
||||
* $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.17 2005/06/21 04:02:33 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -209,7 +209,7 @@ main(int argc, char *argv[])
|
||||
appendPQExpBuffer(&sql, " NOCREATEUSER");
|
||||
appendPQExpBuffer(&sql, ";\n");
|
||||
|
||||
conn = connectDatabase("template1", host, port, username, password, progname);
|
||||
conn = connectDatabase("postgres", host, port, username, password, progname);
|
||||
|
||||
if (echo)
|
||||
printf("%s", sql.data);
|
||||
|
@@ -5,7 +5,7 @@
|
||||
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/bin/scripts/dropdb.c,v 1.14 2004/12/31 22:03:17 pgsql Exp $
|
||||
* $PostgreSQL: pgsql/src/bin/scripts/dropdb.c,v 1.15 2005/06/21 04:02:33 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -117,7 +117,8 @@ main(int argc, char *argv[])
|
||||
appendPQExpBuffer(&sql, "DROP DATABASE %s;\n",
|
||||
fmtId(dbname));
|
||||
|
||||
conn = connectDatabase("template1", host, port, username, password, progname);
|
||||
conn = connectDatabase(strcmp(dbname, "postgres") == 0 ? "template1" : "postgres",
|
||||
host, port, username, password, progname);
|
||||
|
||||
if (echo)
|
||||
printf("%s", sql.data);
|
||||
|
@@ -5,7 +5,7 @@
|
||||
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/bin/scripts/dropuser.c,v 1.13 2004/12/31 22:03:17 pgsql Exp $
|
||||
* $PostgreSQL: pgsql/src/bin/scripts/dropuser.c,v 1.14 2005/06/21 04:02:33 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -116,7 +116,7 @@ main(int argc, char *argv[])
|
||||
initPQExpBuffer(&sql);
|
||||
appendPQExpBuffer(&sql, "DROP USER %s;\n", fmtId(dropuser));
|
||||
|
||||
conn = connectDatabase("template1", host, port, username, password, progname);
|
||||
conn = connectDatabase("postgres", host, port, username, password, progname);
|
||||
|
||||
if (echo)
|
||||
printf("%s", sql.data);
|
||||
|
@@ -5,7 +5,7 @@
|
||||
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/bin/scripts/vacuumdb.c,v 1.12 2004/12/31 22:03:17 pgsql Exp $
|
||||
* $PostgreSQL: pgsql/src/bin/scripts/vacuumdb.c,v 1.13 2005/06/21 04:02:33 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -233,7 +233,7 @@ vacuum_all_databases(bool full, bool verbose, bool analyze,
|
||||
PGresult *result;
|
||||
int i;
|
||||
|
||||
conn = connectDatabase("template1", host, port, username, password, progname);
|
||||
conn = connectDatabase("postgres", host, port, username, password, progname);
|
||||
result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn;", progname, echo);
|
||||
PQfinish(conn);
|
||||
|
||||
|
@@ -37,7 +37,7 @@
|
||||
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.279 2005/06/20 10:29:37 teodor Exp $
|
||||
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.280 2005/06/21 04:02:33 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -53,6 +53,6 @@
|
||||
*/
|
||||
|
||||
/* yyyymmddN */
|
||||
#define CATALOG_VERSION_NO 200506201
|
||||
#define CATALOG_VERSION_NO 200506202
|
||||
|
||||
#endif
|
||||
|
@@ -13,7 +13,7 @@ int main()
|
||||
if (getenv("SQLOPT")) ECPGdebug(1,stderr);
|
||||
|
||||
exec sql whenever sqlerror do sqlprint();
|
||||
exec sql connect to template1;
|
||||
exec sql connect to postgres;
|
||||
|
||||
exec sql allocate descriptor mydesc;
|
||||
exec sql select tablename into descriptor mydesc from pg_tables;
|
||||
|
@@ -4,13 +4,13 @@
|
||||
# A service is a set of named connection parameters. You may specify
|
||||
# multiple services in this file. Each starts with a service name in
|
||||
# brackets. Subsequent lines have connection configuration parameters of
|
||||
# the pattern "param=value". A sample configuration for template1 is
|
||||
# the pattern "param=value". A sample configuration for postgres is
|
||||
# included in this file. Lines beginning with '#' are comments.
|
||||
#
|
||||
# Copy this to your sysconf directory (typically /usr/local/pgsql/etc) and
|
||||
# rename it pg_service.conf.
|
||||
#
|
||||
#
|
||||
#[template1]
|
||||
#dbname=template1
|
||||
#[postgres]
|
||||
#dbname=postgres
|
||||
#user=postgres
|
||||
|
@@ -1,5 +1,5 @@
|
||||
#!/bin/sh
|
||||
# $PostgreSQL: pgsql/src/test/bench/create.sh,v 1.4 2004/09/01 17:25:40 tgl Exp $
|
||||
# $PostgreSQL: pgsql/src/test/bench/create.sh,v 1.5 2005/06/21 04:02:34 tgl Exp $
|
||||
#
|
||||
if [ ! -d $1 ]; then
|
||||
echo " you must specify a valid data directory " >&2
|
||||
@@ -10,10 +10,10 @@ if [ -d ./obj ]; then
|
||||
fi
|
||||
|
||||
echo =============== destroying old bench database... =================
|
||||
echo "drop database bench" | postgres -D${1} template1 > /dev/null
|
||||
echo "drop database bench" | postgres -D${1} postgres > /dev/null
|
||||
|
||||
echo =============== creating new bench database... =================
|
||||
echo "create database bench" | postgres -D${1} template1 > /dev/null
|
||||
echo "create database bench" | postgres -D${1} postgres > /dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
echo createdb failed
|
||||
exit 1
|
||||
|
@@ -26,14 +26,14 @@ main(int argc, char **argv)
|
||||
|
||||
/*
|
||||
* If the user supplies a parameter on the command line, use it as the
|
||||
* conninfo string; otherwise default to setting dbname=template1 and
|
||||
* conninfo string; otherwise default to setting dbname=postgres and
|
||||
* using environment variables or defaults for all other connection
|
||||
* parameters.
|
||||
*/
|
||||
if (argc > 1)
|
||||
conninfo = argv[1];
|
||||
else
|
||||
conninfo = "dbname = template1";
|
||||
conninfo = "dbname = postgres";
|
||||
|
||||
/* Make a connection to the database */
|
||||
conn = PQconnectdb(conninfo);
|
||||
|
@@ -46,14 +46,14 @@ main(int argc, char **argv)
|
||||
|
||||
/*
|
||||
* If the user supplies a parameter on the command line, use it as the
|
||||
* conninfo string; otherwise default to setting dbname=template1 and
|
||||
* conninfo string; otherwise default to setting dbname=postgres and
|
||||
* using environment variables or defaults for all other connection
|
||||
* parameters.
|
||||
*/
|
||||
if (argc > 1)
|
||||
conninfo = argv[1];
|
||||
else
|
||||
conninfo = "dbname = template1";
|
||||
conninfo = "dbname = postgres";
|
||||
|
||||
/* Make a connection to the database */
|
||||
conn = PQconnectdb(conninfo);
|
||||
|
@@ -51,14 +51,14 @@ main(int argc, char **argv)
|
||||
|
||||
/*
|
||||
* If the user supplies a parameter on the command line, use it as the
|
||||
* conninfo string; otherwise default to setting dbname=template1 and
|
||||
* conninfo string; otherwise default to setting dbname=postgres and
|
||||
* using environment variables or defaults for all other connection
|
||||
* parameters.
|
||||
*/
|
||||
if (argc > 1)
|
||||
conninfo = argv[1];
|
||||
else
|
||||
conninfo = "dbname = template1";
|
||||
conninfo = "dbname = postgres";
|
||||
|
||||
/* Make a connection to the database */
|
||||
conn = PQconnectdb(conninfo);
|
||||
|
@@ -1,5 +1,5 @@
|
||||
#! /bin/sh
|
||||
# $PostgreSQL: pgsql/src/test/regress/pg_regress.sh,v 1.56 2005/06/20 02:26:50 tgl Exp $
|
||||
# $PostgreSQL: pgsql/src/test/regress/pg_regress.sh,v 1.57 2005/06/21 04:02:34 tgl Exp $
|
||||
|
||||
me=`basename $0`
|
||||
: ${TMPDIR=/tmp}
|
||||
@@ -441,7 +441,7 @@ then
|
||||
# wait forever, however.
|
||||
i=0
|
||||
max=60
|
||||
until "$bindir/psql" -X $psql_options template1 </dev/null 2>/dev/null
|
||||
until "$bindir/psql" -X $psql_options postgres </dev/null 2>/dev/null
|
||||
do
|
||||
i=`expr $i + 1`
|
||||
if [ $i -ge $max ]
|
||||
|
Reference in New Issue
Block a user