Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfce2c41

Browse files
committed
Updated pg_dumpall and psql to preserve database owners.
1 parentcb8396d commitfce2c41

File tree

3 files changed

+74
-24
lines changed

3 files changed

+74
-24
lines changed

‎src/bin/pg_dump/pg_dumpall

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,27 @@ then
1212
else
1313
BS='\\'# System V
1414
fi
15-
psql -l -A -q -t| tr'|'''| grep -v'^template1'| \
16-
whileread DATABASE USERID USER
17-
do
18-
echo"${BS}connect template1"
19-
echo"create database$DATABASE;"
20-
echo"${BS}connect$DATABASE"
21-
pg_dump"$@"$DATABASE
22-
done
23-
echo"${BS}connect template1"
24-
echo"copy pg_user from stdin;"
2515
#
2616
# Dump everyone but the postgres user
2717
# initdb creates him
2818
#
19+
# get the postgres user id
20+
#
2921
POSTGRES_SUPER_USER_ID="`psql -A -q -t template1<<END
30-
select datdba
22+
select datdba
3123
from pg_database
3224
where datname = 'template1';
3325
END`"
26+
echo"${BS}connect template1"
27+
#
28+
# delete all users in case they run this twice
29+
#
30+
echo"delete from pg_user"
31+
echo"where usesysid<>$POSTGRES_SUPER_USER_ID;"
32+
#
33+
# load all the non-postgres users
34+
#
35+
echo"copy pg_user from stdin;"
3436
psql -q template1 <<END
3537
select pg_user.* into table tmp_pg_user
3638
from pg_user
@@ -39,3 +41,18 @@ copy tmp_pg_user to stdout;
3941
drop table tmp_pg_user;
4042
END
4143
echo"${BS}."
44+
psql -l -A -q -t| tr '|' ' ' | grep -v '^template1 ' |\
45+
while read DATABASE PGUSERID DATAPATH
46+
do
47+
POSTGRES_USER="`psql -A -q -t template1 <<END
48+
select usename
49+
from pg_user
50+
where usesysid =$PGUSERID;
51+
END`"
52+
53+
echo"${BS}connect template1$POSTGRES_USER"
54+
echo"create database$DATABASE;"
55+
echo"${BS}connect$DATABASE$POSTGRES_USER"
56+
pg_dump"$@"$DATABASE
57+
done
58+
# done

‎src/bin/psql/psql.c

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.59 1997/04/10 11:54:29 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.60 1997/05/21 03:12:02 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#include<stdio.h>
15+
#include<stdlib.h>
1516
#include<string.h>
1617
#include<signal.h>
1718
#include<errno.h>
@@ -155,7 +156,7 @@ slashUsage(PsqlSettings * ps)
155156
fprintf(stderr," \\? -- help\n");
156157
fprintf(stderr," \\a -- toggle field-alignment (currenty %s)\n",on(ps->opt.align));
157158
fprintf(stderr," \\C [<captn>] -- set html3 caption (currently '%s')\n",ps->opt.caption ?ps->opt.caption :"");
158-
fprintf(stderr," \\connect <dbname> -- connect to new database (currently '%s')\n",PQdb(ps->db));
159+
fprintf(stderr," \\connect <dbname><user> -- connect to new database (currently '%s')\n",PQdb(ps->db));
159160
fprintf(stderr," \\copy table {from | to} <fname>\n");
160161
fprintf(stderr," \\d [<table>] -- list tables in database or columns in <table>, * for all\n");
161162
fprintf(stderr," \\e [<fname>] -- edit the current query buffer or <fname>, \\E execute too\n");
@@ -825,19 +826,36 @@ do_copy(const char *args, PsqlSettings * settings)
825826

826827

827828
staticvoid
828-
do_connect(constchar*new_dbname,PsqlSettings*settings)
829+
do_connect(constchar*new_dbname,
830+
constchar*new_user,
831+
PsqlSettings*settings)
829832
{
830833

831834
char*dbname=PQdb(settings->db);
832835
if (!new_dbname)
833836
fprintf(stderr,"\\connect must be followed by a database name\n");
834837
else {
835-
PGconn*olddb=settings->db;
838+
PGconn*olddb=settings->db;
839+
char*userenv;
836840

837841
printf("closing connection to database: %s\n",dbname);
842+
if (new_user!=NULL) {
843+
/*
844+
PQsetdb() does not allow us to specify the user,
845+
so we have to do it via PGUSER
846+
*/
847+
userenv=malloc(strlen("PGUSER=")+strlen(new_user)+1);
848+
sprintf(userenv,"PGUSER=%s",new_user);
849+
putenv(userenv);
850+
free(userenv);
851+
}
838852
settings->db=PQsetdb(PQhost(olddb),PQport(olddb),
839853
NULL,NULL,new_dbname);
840-
printf("connecting to new database: %s\n",new_dbname);
854+
if (!new_user)
855+
printf("connecting to new database: %s\n",new_dbname);
856+
else
857+
printf("connecting to new database: %s as user: %s\n",
858+
new_dbname,new_user);
841859
if (PQstatus(settings->db)==CONNECTION_BAD) {
842860
fprintf(stderr,"%s\n",PQerrorMessage(settings->db));
843861
printf("reconnecting to %s\n",dbname);
@@ -1037,12 +1055,19 @@ HandleSlashCmds(PsqlSettings * settings,
10371055
* assuming it's not a one-character command. If it's a one-character
10381056
* command, this is meaningless.
10391057
*/
1058+
char*optarg3;
1059+
/*
1060+
* Pointer inside the second <cmd> string to the argument of the slash command
1061+
* assuming it's not a one-character command. If it's a one-character
1062+
* command, this is meaningless.
1063+
*/
10401064
char*cmd;
10411065
/*
10421066
* String: value of the slash command, less the slash and with escape
10431067
* sequences decoded.
10441068
*/
10451069
intblank_loc;
1070+
intblank_loc2;
10461071
/* Offset within <cmd> of first blank */
10471072

10481073
cmd=malloc(strlen(line));/* unescaping better not make string grow. */
@@ -1064,12 +1089,20 @@ HandleSlashCmds(PsqlSettings * settings,
10641089
optarg=NULL;
10651090

10661091
blank_loc=strcspn(cmd," \t");
1067-
if (blank_loc==0)
1092+
if (blank_loc==0) {
10681093
optarg2=NULL;
1069-
else
1094+
optarg3=NULL;
1095+
}else {
10701096
optarg2=cmd+blank_loc+strspn(cmd+blank_loc," \t");
1071-
1072-
1097+
blank_loc2=strcspn(optarg2," \t");
1098+
if (blank_loc2==0||*(optarg2+blank_loc2)=='\0')
1099+
optarg3=NULL;
1100+
else {
1101+
optarg3=optarg2+blank_loc2+strspn(optarg2+blank_loc2," \t");
1102+
*(optarg2+blank_loc2)='\0';
1103+
}
1104+
}
1105+
10731106
switch (cmd[0]) {
10741107
case'a':/* toggles to align fields on output */
10751108
toggle(settings,&settings->opt.align,"field alignment");
@@ -1092,9 +1125,9 @@ HandleSlashCmds(PsqlSettings * settings,
10921125
if (strncmp(cmd,"copy ",strlen("copy "))==0)
10931126
do_copy(optarg2,settings);
10941127
elseif (strncmp(cmd,"connect ",strlen("connect "))==0)
1095-
do_connect(optarg2,settings);
1128+
do_connect(optarg2,optarg3,settings);
10961129
else
1097-
do_connect(optarg,settings);
1130+
do_connect(optarg,optarg2,settings);
10981131
}
10991132
break;
11001133
case'd':/* \d describe tables or columns in a table */

‎src/man/psql.1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.\" This is -*-nroff-*-
22
.\" XXX standard disclaimer belongs here....
3-
.\" $Header: /cvsroot/pgsql/src/man/Attic/psql.1,v 1.7 1997/04/10 11:58:59 scrappy Exp $
3+
.\" $Header: /cvsroot/pgsql/src/man/Attic/psql.1,v 1.8 1997/05/21 03:12:23 momjian Exp $
44
.TH PSQL UNIX 1/20/96 PostgreSQL PostgreSQL
55
.SH NAME
66
psql\(em run the interactive query front-end
@@ -258,7 +258,7 @@ You should anyway.
258258
Toggle field alignment when printing out table elements.
259259
.IP"\eC\fIcaption\fR"
260260
Set the HTML3.0 table caption.
261-
.IP"\econnect\fIdbname\fR"
261+
.IP"\econnect\fIdbname\fR\fIusername\fR"
262262
Establish a connection to a new database. The previous connection is closed.
263263
.IP"\ecopy\fItable\fR {FROM | TO}\fIfilename\fR"
264264
Perform a frontend copy. This is an operation that runs a SQL COPY command,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp