Starting postmaster
Nothing can happen to a database unless the
postmaster
process is running. As the site administrator, there
are a number of things you should remember before
starting the postmaster.
These are discussed in the installation and configuration sections
of this manual.
However, if Postgres has been installed by following
the installation instructions exactly as written, the
following simple command is all you should
need to start the postmaster:
% postmaster
The postmaster occasionally prints out
messages which
are often helpful during troubleshooting. If you wish
to view debugging messages from the postmaster,
you can
start it with the -d option and redirect the output to
the log file:
% postmaster -d >& pm.log &
If you do not wish to see these messages, you can type
% postmaster -S
and the postmaster will be "S"ilent.
Notice that there
is no ampersand ("&") at the end of the last example.
Adding and Deleting Users
createuser enables specific users to access
Postgres.
destroyuser removes users and
prevents them from accessing Postgres.
Note that these
commands only affect users with respect to
Postgres;
they have no effect on users other privileges or status with regards
to the underlying
operating system.
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.
Either an absolute path name or an environment variable
may be specified as a location. Note that for security and integrity reasons,
all paths and environment variables so specified have some
additional path fields appended.
The environment variable style of specification
is to be preferred since it allows the site administrator more flexibility in
managing disk storage.
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, type
% setenv PGDATA2 /home/postgres/data
Usually, you will want to define this variable in the
Postgres superuser's
.profile
or
.cshrc
initialization file to ensure that it is defined upon system startup.
To create a data storage area in /home/postgres/data, ensure
that /home/postgres already exists and is writable.
From the command line, type
% 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
% destroydb test
Troubleshooting
Assuming that your site administrator has properly
started the postmaster process
and authorized you to
use the database, you (as a user) may begin to start up
applications. As previously mentioned, you should add
/usr/local/pgsql/bin to your shell search path.
In most cases, this is all you should have to do in
terms of preparation.
If you get the following error message from a
Postgres
command (such as psql or
createdb):
connectDB() failed: Is the postmaster running at 'localhost' on port '5432'?
it is usually because either the postmaster is not running,
or you are attempting to connect to the wrong server host.
If you get the following error message:
FATAL 1:Feb 17 23:19:55:process userid (2360) != database owner (268)
it means that the site administrator started the postmaster
as the wrong user. Tell him to restart it as
the Postgres superuser.
Managing a Database
Now that Postgres is up and running we can create
some databases to experiment with. Here, we describe the
basic commands for managing a database.
Creating a Database
Let's say you want to create a database named mydb.
You can do this with the following command:
% createdb mydb
Postgres 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 Postgres
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.
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 mydb
database by typing the command:
% psql mydb
You will be greeted with the following message:
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: mydb
mydb=>
This prompt indicates that the terminal monitor is listening
to you and that you can type SQL queries into a
workspace maintained by the terminal monitor.
The psql program responds to escape
codes that begin
with the backslash character, "\". For example, you
can get help on the syntax of various
Postgres SQL commands by typing:
mydb=> \h
Once you have finished entering your queries into the
workspace, you can pass the contents of the workspace
to the Postgres server by typing:
mydb=> \g
This tells the server to process the query. If you
terminate your query with a semicolon, the backslash-g is not
necessary. psql will automatically
process semicolon terminated queries.
To read queries from a file, say myFile, instead of
entering them interactively, type:
mydb=> \i fileName
To get out of psql and return to UNIX, type
mydb=> \q
and psql 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 SQL queries.
Single-line comments are denoted by
--
. Everything after the dashes up to the end of the
line is ignored. Multiple-line comments, and comments within a line,
are denoted by /* ... */
Destroying a Database
If you are the database administrator for the database
mydb, you can destroy it using the following UNIX command:
% destroydb mydb
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.