1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
<chapter id="manage-ag">
<title>Managing a Database</title>
<para>
If the <productname>Postgres</productname>
<application>postmaster</application> is up and running we can create
some databases to experiment with. Here, we describe the
basic commands for managing a database.
</para>
<sect1>
<title>Creating a Database</title>
<para>
Let's say you want to create a database named mydb.
You can do this with the following command:
<programlisting>
% createdb <replaceable class="parameter">dbname</replaceable>
</programlisting>
<productname>Postgres</productname> allows you to create
any number of databases
at a given site and you automatically become the
database administrator of the database you just created.
Database names must have an alphabetic first
character and are limited to 16 characters in length.
Not every user has authorization to become a database
administrator. If <productname>Postgres</productname>
refuses to create databases
for you, then the site administrator needs to grant you
permission to create databases. Consult your site
administrator if this occurs.
</para>
</sect1>
<sect1>
<title>Accessing a Database</title>
<para>
Once you have constructed a database, you can access it
by:
<itemizedlist spacing="compact" mark="bullet">
<listitem>
<para>
running the <productname>Postgres</productname> terminal monitor program
(<application>psql</application>) which allows you to interactively
enter, edit, and execute <acronym>SQL</acronym> commands.
</para>
</listitem>
<listitem>
<para>
writing a C program using the <literal>libpq</literal> subroutine
library. This allows you to submit <acronym>SQL</acronym> commands
from C and get answers and status messages back to
your program. This interface is discussed further
in the <citetitle>PostgreSQL Programmer's Guide</citetitle>.
</para>
</listitem>
</itemizedlist>
You might want to start up <application>psql</application>,
to try out the examples in this manual. It can be activated for the
<replaceable class="parameter">dbname</replaceable> database by typing the command:
<programlisting>
% psql <replaceable class="parameter">dbname</replaceable>
</programlisting>
You will be greeted with the following message:
<programlisting>
Welcome to the Postgres interactive sql monitor:
type \? for help on slash commands
type \q to quit
type \g or terminate with semicolon to execute query
You are currently connected to the database: <replaceable>dbname</replaceable>
<replaceable>dbname</replaceable>=>
</programlisting>
</para>
<para>
This prompt indicates that the terminal monitor is listening
to you and that you can type <acronym>SQL</acronym> queries into a
workspace maintained by the terminal monitor.
The <application>psql</application> program responds to escape
codes that begin
with the backslash character, "\". For example, you
can get help on the syntax of various
<productname>Postgres</productname> <acronym>SQL</acronym> commands by typing:
<programlisting>
<replaceable>dbname</replaceable>=> \h
</programlisting>
Once you have finished entering your queries into the
workspace, you can pass the contents of the workspace
to the <productname>Postgres</productname> server by typing:
<programlisting>
<replaceable>dbname</replaceable>=> \g
</programlisting>
This tells the server to process the query. If you
terminate your query with a semicolon, the backslash-g is not
necessary. <application>psql</application> will automatically
process semicolon terminated queries.
To read queries from a file, instead of
entering them interactively, type:
<programlisting>
<replaceable>dbname</replaceable>=> \i <replaceable class="parameter">filename</replaceable>
</programlisting>
To get out of <application>psql</application> and return to UNIX, type
<programlisting>
<replaceable>dbname</replaceable>=> \q
</programlisting>
and <application>psql</application> will quit and return
you to your command shell. (For more escape codes, type
backslash-h at the monitor prompt.)
White space (i.e., spaces, tabs and newlines) may be
used freely in <acronym>SQL</acronym> queries.
Single-line comments are denoted by two dashes
(<quote>--</quote>). Everything after the dashes up to the end of the
line is ignored. Multiple-line comments, and comments within a line,
are denoted by <quote>/* ... */</quote>, a convention borrowed
from <productname>Ingres</productname>.
</para>
</sect1>
<sect1>
<title>Destroying a Database</title>
<para>
If you are the database administrator for the database
mydb, you can destroy it using the following UNIX command:
<programlisting>
% destroydb <replaceable class="parameter">dbname</replaceable>
</programlisting>
This action physically removes all of the UNIX files
associated with the database and cannot be undone, so
this should only be done with a great deal of forethought.
</para>
<para>
It is also possible to destroy a database from within an
<acronym>SQL</acronym> session by using
<programlisting>
> drop database <replaceable class="parameter">dbname</replaceable>
</programlisting>
</para>
</sect1>
<sect1>
<title>Backup and Restore</title>
<caution>
<para>
Every database should be backed up on a regular basis. Since
<productname>Postgres</productname> manages it's own files in the
file system, it is <emphasis>not advisable</emphasis> to rely on
system backups of your file system for your database backups;
there is no guarantee that the files will be in a usable,
consistant state after restoration.
</para>
</caution>
<para>
<productname>Postgres</productname> provides two utilities to
backup your system: <application>pg_dump</application> to backup
individual databases and
<application>pg_dumpall</application> to backup your installation
in one step.
</para>
<para>
An individual database can be backed up using the following
command:
<programlisting>
% pg_dump <replaceable class="parameter">dbname</replaceable> > <replaceable class="parameter">dbname</replaceable>.pgdump
</programlisting>
and can be restored using
<programlisting>
cat <replaceable class="parameter">dbname</replaceable>.pgdump | psql <replaceable class="parameter">dbname</replaceable>
</programlisting>
</para>
<para>
This technique can be used to move databases to new
locations, and to rename existing databases.
</para>
<sect2>
<title>Large Databases</title>
<note>
<title>Author</title>
<para>
Written by <ulink url="hannu@trust.ee">Hannu Krosing</ulink> on
1999-06-19.
</para>
</note>
<para>
Since <productname>Postgres</productname> allows tables larger
than the maximum file size on your system, it can be problematic
to dump the table to a file, since the resulting file will likely
be larger than the maximum size allowed by your system.</para>
<para>
As <application>pg_dump</application> writes to stdout,
you can just use standard *nix tools
to work around this possible problem:
<itemizedlist>
<listitem>
<para>
Use compressed dumps:
<programlisting>
% pg_dump <replaceable class="parameter">dbname</replaceable> | gzip > <replaceable class="parameter">filename</replaceable>.dump.gz
</programlisting>
reload with
<programlisting>
% createdb <replaceable class="parameter">dbname</replaceable>
% gunzip -c <replaceable class="parameter">filename</replaceable>.dump.gz | psql <replaceable class="parameter">dbname</replaceable>
</programlisting>
or
<programlisting>
% cat <replaceable class="parameter">filename</replaceable>.dump.gz | gunzip | psql <replaceable class="parameter">dbname</replaceable>
</programlisting>
</para>
</listitem>
<listitem>
<para>
Use split:
<programlisting>
% pg_dump <replaceable class="parameter">dbname</replaceable> | split -b 1m - <replaceable class="parameter">filename</replaceable>.dump.
</programlisting>
reload with
<programlisting>
% createdb <replaceable class="parameter">dbname</replaceable>
% cat <replaceable class="parameter">filename</replaceable>.dump.* | pgsql <replaceable class="parameter">dbname</replaceable>
</programlisting>
</para>
</listitem>
</itemizedlist>
</para>
<para>
Of course, the name of the file
(<replaceable class="parameter">filename</replaceable>) and the
content of the <application>pg_dump</application> output need not
match the name of the database. Also, the restored database can
have an arbitrary new name, so this mechanism is also suitable
for renaming databases.
</para>
</sect2>
</sect1>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"./reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/CATALOG"
sgml-local-ecat-files:nil
End:
-->
|