Monday, July 16, 2012

PostgreSQL: Database Creation & Administration




Database Cluster
-        a database storage area on disk. (SQL uses the term catalog cluster.)
-       a collection of databases that is managed by a single instance of a running database server.
-       After initialization, a database cluster will contain a database named postgreswhich is meant as a default database for use by utilities, users and third party applications.
-       Another database created within each cluster during initialization is called template1. As the name suggests, this will be used as a template for subsequently created databases; it should not be used for actual work.
-       In file system terms, a database cluster will be a single directory under which all data will be stored. We call this the data directory or data area.
-       Initdb : To initialize a database cluster



Creating a Database Cluster

-bash-3.00$ initdb -D /u01/postgres/postgres/9.0-pgdg/data
The files belonging to this database system will be owned by user "postgresql".
This user must also own the server process.

The database cluster will be initialized with locale C.
The default database encoding has accordingly been set to SQL_ASCII.
The default text search configuration will be set to "english".

creating directory /u01/postgres/postgres/9.0-pgdg/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 32MB
creating configuration files ... ok
creating template1 database in /u01/postgres/postgres/9.0-pgdg/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

    postgres -D /u01/postgres/postgres/9.0-pgdg/data
or
    pg_ctl -D /u01/postgres/postgres/9.0-pgdg/data -l logfile start

-bash-3.00$
Alternatively, you can run initdb via the pg_ctl program like so:
$ pg_ctl   -D    /u01/postgres/postgres/9.0-pgdg/data   initdb


Best practice:
root# mkdir /usr/local/pgsql/data
root# chown postgres /usr/local/pgsql/data
root# su postgres
postgres$ initdb -D /usr/local/pgsql/data


Starting/stopping Database Server/Cluster

Command to start:  pg_ctl -D /u01/postgres/postgres/9.0-pgdg/data -l logfile start
                                  pg_ctl -D /u01/postgres/postgres/9.0-pgdg/data start

Command to stop:  pg_ctl -D /u01/postgres/postgres/9.0-pgdg/data -l logfile stop
                                   pg_ctl -D /u01/postgres/postgres/9.0-pgdg/data -m f stop

Starting/stopping with a different port:
In postgresql.conf file
listen_addresses = '127.0.0.1'  
port = 5433
Localhost does not work here

Also, put PGPORT = 5433 in .bash_profile

pg_ctl -D /u01/postgres/postgres/9.0-pgdg/data –m f stop
pg_ctl -D /u01/postgres/postgres/9.0-pgdg/data -o "-F -p 5433" start

-bash-3.00$
-bash-3.00$
-bash-3.00$ ps -ef|grep postgres
postgres 21992 21565   0 18:56:22 pts/1       0:00 grep postgres
postgres 21991 21565   0 18:56:22 pts/1       0:00 ps -ef
postgres 21565 21534   0 18:55:18 pts/1       0:00 -bash
postgres 21534 21533   0 18:55:07 ?           0:00 /usr/lib/ssh/sshd
-bash-3.00$
-bash-3.00$
-bash-3.00$ pg_ctl -D /u01/postgres/postgres/9.0-pgdg/data -l logfile start
postmaster starting
-bash-3.00$
-bash-3.00$
-bash-3.00$ ps -ef|grep postgres
postgres 22044 21565   0 18:56:39 pts/1       0:00 ps -ef
postgres 22045 21565   0 18:56:39 pts/1       0:00 grep postgres
postgres 21565 21534   0 18:55:18 pts/1       0:00 -bash
postgres 21534 21533   0 18:55:07 ?           0:00 /usr/lib/ssh/sshd
-bash-3.00$
-bash-3.00$
-bash-3.00$ echo $PATH
/usr/bin:/opt/EMCpower/bin:/etc/emc/bin:/etc
-bash-3.00$
-bash-3.00$ cd /u01/postgres/postgres/9.0-pgdg/bin/64/
-bash-3.00$
-bash-3.00$ pwd
/u01/postgres/postgres/9.0-pgdg/bin/64
-bash-3.00$
-bash-3.00$ export PATH=/u01/postgres/postgres/9.0-pgdg/bin/64:/usr/bin:/opt/EMCpower/bin:/etc/emc/bin:/etc
-bash-3.00$
-bash-3.00$ pg_ctl -D /u01/postgres/postgres/9.0-pgdg/data -l logfile start
server starting
-bash-3.00$
-bash-3.00$ ps -ef|grep postgres
postgres 22692 21565   0 18:58:47 pts/1       0:00 ps -ef
postgres 22657 22652   0 18:58:37 ?           0:00 /u01/postgres/postgres/9.0-pgdg/bin/64/postgres -D /u01/postgres/postgres/9.0-p
postgres 22652     1   0 18:58:37 pts/1       0:00 /u01/postgres/postgres/9.0-pgdg/bin/64/postgres -D /u01/postgres/postgres/9.0-p
postgres 22655 22652   0 18:58:37 ?           0:00 /u01/postgres/postgres/9.0-pgdg/bin/64/postgres -D /u01/postgres/postgres/9.0-p
postgres 22656 22652   0 18:58:37 ?           0:00 /u01/postgres/postgres/9.0-pgdg/bin/64/postgres -D /u01/postgres/postgres/9.0-p
postgres 21565 21534   0 18:55:18 pts/1       0:00 -bash
postgres 21534 21533   0 18:55:07 ?           0:00 /usr/lib/ssh/sshd
postgres 22654 22652   0 18:58:37 ?           0:00 /u01/postgres/postgres/9.0-pgdg/bin/64/postgres -D /u01/postgres/postgres/9.0-p
postgres 22693 21565   0 18:58:47 pts/1       0:00 grep postgres
-bash-3.00$


Connecting to psql

-bash-3.00$ export PATH=/u01/postgres/postgres/9.0-pgdg/bin/64:/usr/bin:/opt/EMCpower/bin:/etc/emc/bin:/etc
-bash-3.00$
-bash-3.00$ psql postgres
psql (9.0.4)
Type "help" for help.

postgres=#



]]]]]

Important Configuration Files:
postgresql.conf (under /u01/postgres/postgres/9.0-pgdg/data)
listen_addresses : from which address server will respond

pg_hba.conf (under /u01/postgres/postgres/9.0-pgdg/data)
host based authentication

                    [[[[[



No comments:

Post a Comment