+ linkend="postgres-user">). Then from the command line, type
+
+initlocation PGDATA2
+
+
+ The you can restart the server.
-
-
-
Accessing a Database
+ To create a database at the new location, use the command
+
+CREATE DATABASE name> WITH LOCATION = 'location>'
+
+ where location> is the environment variable you
+ used, PGDATA2> in this example. The createdb>
+ command has the option
+
- Once you have constructed a database, you can access it
- by:
-
-
-
- running the
Postgres terminal monitor program
- (
psql) which allows you to interactively
- enter, edit, and execute
SQL commands.
-
-
-
-
- writing a C program using the libpq subroutine
- library. This allows you to submit
SQL commands
- from C and get answers and status messages back to
- your program. This interface is discussed further
- in the PostgreSQL Programmer's Guide.
-
-
-
-
- You might want to start up
psql,
- to try out the examples in this manual. It can be activated for the
- dbname database by typing the command:
+ Database created at alternative locations using this method can be
+ accessed and dropped like any other database.
+
+
+
+ It can also be possible to specify absolute paths directly to the
+ CREATE DATABASE> command without defining environment
+ variables. This is disallowed by default because it is a security
+ risk. To allow it, you must compile
Postgres> with
+ the C preprocessor macro ALLOW_ABSOLUTE_DBPATHS>
+ defined. One way to do this is to run the compilation step like
+ this: gmake COPT=-DALLOW_ABSOLUTE_DBPATHS all>.
+
+
+
+
+
+
+
+
Accessing a Database
+
+ Once you have constructed a database, you can access it by:
+
+
+
+ running the
Postgres terminal monitor program
+ (
psql) which allows you to interactively
+ enter, edit, and execute
SQL commands.
+
+
+
+
+ writing a C program using the libpq subroutine
+ library. This allows you to submit
SQL commands
+ from C and get answers and status messages back to
+ your program. This interface is discussed further
+ in the PostgreSQL Programmer's Guide.
+
+
+
+
+ You might want to start up
psql,
+ to try out the examples in this manual. It can be activated for the
+ dbname database by typing the command:
psql dbname
- You will be greeted with the following message:
+ You will be greeted with the following message:
Welcome to psql, the PostgreSQL interactive terminal.
are denoted by "/* ... */", a convention borrowed
-
+
-
-
Destroying a Database
+
+
Destroying a Database
- If you are the database administrator for the database
- mydb, you can destroy it using the following Unix command:
-
-% dropdb dbname
-
-
- 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.
-
-
- It is also possible to destroy a database from within an
-
-> drop database dbname
-
-
-
-
-
-
Backup and Restore
-
-
- Every database should be backed up on a regular basis. Since
-
Postgres manages it's own files in the
- file system, it is not advisable 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.
-
-
-
-
Postgres provides two utilities to
- backup your system:
pg_dump to backup
- individual databases and
-
pg_dumpall to backup your installation
- in one step.
-
-
- An individual database can be backed up using the following
- command:
-
-% pg_dump dbname > dbname.pgdump
-
-
- and can be restored using
-
-cat dbname.pgdump | psql dbname
-
-
-
- This technique can be used to move databases to new
- locations, and to rename existing databases.
-
-
-
-
Large Databases
-
-
-
Author
- Written by
Hannu Krosing on
- 1999-06-19.
-
-
-
- Since
Postgres 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.
+ Databases are destroyed with the command DROP DATABASE:
+
+DROP DATABASE name>
+
+ Only the owner of the database (i.e., the user that created it) can
+ drop databases. Dropping a databases removes all objects that were
+ contained within the database. The destruction of a database cannot
+ be undone.
+
- As
pg_dump writes to stdout,
- you can just use standard *nix tools
- to work around this possible problem:
-
-
-
- Use compressed dumps:
-
-% pg_dump dbname | gzip > filename.dump.gz
-
-
- reload with
-
-% createdb dbname
-% gunzip -c filename.dump.gz | psql dbname
-
-
-or
-
-% cat filename.dump.gz | gunzip | psql dbname
-
-
-
-
-
- Use split:
-
-% pg_dump dbname | split -b 1m - filename.dump.
-
-
-reload with
-
-% createdb dbname
-% cat filename.dump.* | pgsql dbname
-
-
-
-
-
+ You cannot execute the DROP DATABASE command
+ while connected to the victim database. You can, however, be
+ connected to any other database, including the template1 database,
+ which would be the only option for dropping the last database of a
+ given cluster.
+
- Of course, the name of the file
- (filename) and the
- content of the
pg_dump 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.
-
-
-
+ For convenience, there is also a shell program to drop databases:
+
+dropdb dbname
+
+ (Unlike createdb>, it is not the default action to drop
+ the database with the current user name.)
+
+
-
-
-
Disk Management
-
-
-
Alternate Locations
-
- It is possible to create a database in a location other than the default
- location for the installation. Remember that all database access actually
- occurs through the database backend, so that any location specified must
- be accessible by the backend.
-
-
- Alternate database locations are created and referenced by an environment variable
- which gives the absolute path to the intended storage location.
- This environment variable must have been defined before the backend was started
- and must be writable by the postgres administrator account.
- Any valid environment variable name may be used to reference an alternate
- location, although using variable name with a prefix of PGDATA is recommended
- to avoid confusion and conflict with other variables.
-
-
-
- In previous versions of
Postgres,
- it was also permissable to use an absolute path name
- to specify an alternate storage location.
- The environment variable style of specification
- is to be preferred since it allows the site administrator more flexibility in
- managing disk storage.
- If you prefer using absolute paths, you may do so by defining
- "ALLOW_ABSOLUTE_DBPATHS" and recompiling
Postgres
- To do this, either add this line
-
-#define ALLOW_ABSOLUTE_DBPATHS 1
-
-
- to the file src/include/config.h, or by specifying
-
- CFLAGS+= -DALLOW_ABSOLUTE_DBPATHS
-
-
- in your Makefile.custom.
-
-
-
- Remember that database creation is actually performed by the database backend.
- Therefore, any environment variable specifying an alternate location must have
- been defined before the backend was started. To define an alternate location
- PGDATA2 pointing to /home/postgres/data, first type
-
-% setenv PGDATA2 /home/postgres/data
-
-
- to define the environment variable to be used with subsequent commands.
- Usually, you will want to define this variable in the
- .profile
- or
- .cshrc
- initialization file to ensure that it is defined upon system startup.
- Any environment variable can be used to reference alternate location,
- although it is preferred that the variables be prefixed with "PGDATA"
- to eliminate confusion and the possibility of conflicting with or
- overwriting other variables.
-
-
- To create a data storage area in PGDATA2, ensure
- that /home/postgres already exists and is writable
- by the postgres administrator.
- Then from the command line, type
-
-% setenv PGDATA2 /home/postgres/data
-% initlocation $PGDATA2
-Creating Postgres database system directory /home/postgres/data
-
-Creating Postgres database system directory /home/postgres/data/base
-
-
-
-
- To test the new location, create a database test by typing
-
-% createdb -D PGDATA2 test
-% dropdb test
-
-
-
-
-
-
-