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