Creating and running a database with PostgreSQL
Setup the Run-time Environment
Section titled “Setup the Run-time Environment”On unix, the database files should be owned by a single user, and the database running under that user.
In our setup, we created the user postgres, which will run and own the
database files. It is convenient to add the PostgreSQL binary path to the
PATH environment variable for this user.
Choose a directory for your data. We chose /home/postgres/data
For convenience, we set the environment variable PGDATA to
/home/postgres/data. Otherwise it should be specified with the -D option.
We setup the environment for this user using the following .profile script
export PATH=$PATH:/usr/local/pgsql/binexport PGDATA=/home/postgres/dataIf you are using Cygwin, then you will not need to set your PATH, but
you will need to choose a PGDATA. You will also need to run the
Inter-Process Communication daemon before starting postgres.
ipc-daemon2 &export PGDATA=/cygdrive/d/jurn/postgresdb/dataCreate a database cluster
Section titled “Create a database cluster”A cluster may contain many databases. It is not a cluster of servers.
Initialise the instance.
Section titled “Initialise the instance.”If the environment variables are already set, you can run initdb to initialise a database.
bash-2.05a$ initdbThe files belonging to this database system will be owned by user "postgres".This user must also own the server process.
The database cluster will be initialized with locale C.
creating directory /home/postgres/data... okcreating directory /home/postgres/data/base... okcreating directory /home/postgres/data/global... okcreating directory /home/postgres/data/pg_xlog... okcreating directory /home/postgres/data/pg_clog... okselecting default max_connections... 100selecting default shared_buffers... 1000creating configuration files... okcreating template1 database in /home/postgres/data/base/1... okinitializing pg_shadow... okenabling unlimited row size for system tables... okinitializing pg_depend... okcreating system views... okloading pg_description... okcreating conversions... oksetting privileges on built-in objects... okcreating information schema... okvacuuming database template1... okcopying template1 to template0... ok
Success. You can now start the database server using:
/usr/local/pgsql/bin/postmaster -D /home/postgres/dataor /usr/local/pgsql/bin/pg_ctl -D /home/postgres/data -l logfile startStarting and Stopping the cluster
Section titled “Starting and Stopping the cluster”Before actions such as creating a database can be used, you must start the database.
Starting PostgreSQL
Section titled “Starting PostgreSQL”A basic start command:
bash-2.05a$ pg_ctl start -l /home/postgres/logfilepostmaster successfully startedHowever, this will only allow local connections through unix domain
sockets. If you needed to share this instance over the network, you must
add the -i option to enable TCP connections. If you do this, it will by default
bind to all addresses. Make sure your PostgreSQL installation is not
directly exposed to the internet by binding the ports to the localhost
or lan address by using the -h option. If your PostgreSQL instance
must be accessible from the internet, then utilise ssh to create a
secure tunnel to the host and forward the appropriate ports. The
PostgreSQL Documentation explains
this in detail.
The example below listens on the LAN address 192.168.1.1:
bash-2.05a$ pg_ctl start -l /home/postgres/logfile -o "-i -h 192.168.1.1"postmaster successfully startedThe example below listens on localhost or 127.0.0.1:
bash-2.05a$ pg_ctl start -l /home/postgres/logfile -o "-i -h localhost"postmaster successfully startedStopping PostgreSQL
Section titled “Stopping PostgreSQL”bash-2.05a$ pg_ctl stopwaiting for postmaster to shut down.....donepostmaster successfully shut downSample init.d script
Section titled “Sample init.d script”#!/bin/sh## postgresql## variablesPGDTA=/home/postgres/dataLOGFILE=/home/postgres/logfileOPTIONS="-i -h localhost"PG_CTL=/usr/local/pgsql/bin/pg_ctl
# See how we were called.case "$1" in start) # Start daemon. su - postgres -c "$PG_CTL start -D $PGDTA -l $LOGFILE -o \"$OPTIONS\"" ;; stop) su - postgres -c "$PG_CTL stop -D $PGDTA" ;; status) su - postgres -c "$PG_CTL status -D $PGDTA" ;; reload) su - postgres -c "$PG_CTL reload -D $PGDTA" ;; *) echo "Usage: $0 {start|stop|status|reload}" exit 1esac
exit 0Further Setup
Section titled “Further Setup”See User and Authentication for more information on how to setup users.