3. PostgreSQL Setup : Manuals
PostgreSQL Manuals : all versions
http://www.postgresql.org/docs/manuals/
For PostgreSQL 9.1 - with user comments
http://www.postgresql.org/docs/9.1/interactive/index.html
For PostgreSQL 9.1 - without user comments
http://www.postgresql.org/docs/9.1/static/index.html
www.opengurukul.com 3
5. PostgreSQL Setup : Version
The postgres server version can be found by
using --version on the postmaster
$ postmaster --version
postgres (PostgreSQL) 8.4.5
$
www.opengurukul.com 5
7. pg_ctl
The pg_ctl utility is used to
Initialize PostgreSQL database cluster
Start PostgreSQL database server
Stop PostgreSQL database server
Restart PostgreSQL database server
Display status of a running PostgreSQL database
server
www.opengurukul.com 7
8. pg_ctl : PGDATA variable
The pg_ctl commands are dependent on the
PGDATA variable that contains directory
location of database cluster.
The PGDATA directory location can be specified
using -D in case PGDATA environment
variable is not set.
Generally, we switch to “postgres” user in case
the database cluster was created by “postgres”
user.
www.opengurukul.com 8
9. pg_lsclusters : Location of
Database Clusters : Ubuntu
pg_lsclusters :
$ pg_lsclusters # to clusters and data directory
www.opengurukul.com 9
10. pg_ctl : Control PostgreSQL
Server
Start Server : Stop Server :
$ pg_ctl start $ pg_ctl stop
server starting waiting for server to shut down....
done
$
server stopped
$
Restart Server : Server Status :
$ pg_ctl restart $ pg_ctl status
waiting for server to shut down.... pg_ctl: server is running (PID: 8384)
done
/usr/bin/postgres
server stopped
$
server starting
www.opengurukul.com 10
$
14. PhpPgAdmin : Configure
/usr/share/phpPgAdmin/conf/config.inc.php :
Modify extra_login_security to false in
/usr/share/phpPgAdmin/conf/config.inc.php
If extra login security is true, then logins via phpPgAdmin
with no password or certain usernames (pgsql, postgres,
root, administrator) will be denied.
Set this to FALSE to access it using postgres etc.
Its default value is true.
// $conf['extra_login_security'] = true;
Set it to false
$conf['extra_login_security'] = false;
www.opengurukul.com 14
15. PhpPgAdmin : Configure
pg_hba.conf :
Modify /var/lib/pgsql/data/pg_hba.conf file to change the method to "trust" from
"ident".
New Settings:
local all all trust
Old Settings:
#local all all ident
Restart :
Apache Web Server
Restart PostgreSQL Server
www.opengurukul.com 15
17. PgAdmin: Configure
1> pg_hba.conf : edit pg_hba.conf file (use trust) otherwise you
wiill not be able to connect
# TYPE DATABASE USER CIDR-ADDRESS
METHOD
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
2> restart PostgresSQL Server
3> Configure a Server using PgAdmin3
Host localhost, Port 5432
user – postgres, password - postgres
service – postgresql, database - postgres, edb etc.
www.opengurukul.com 17
4> The configuration can be checked in postgresql.conf
20. psql : command line
$ psql -U<user_name> -d<db_name>
<DB_NAME>=# help
You are using psql, the command-line interface to PostgreSQL.
Type: copyright for distribution terms
h for help with SQL commands
? for help with psql commands
g or terminate with semicolon to execute query
q to quit
<DB_NAME>=# q
$
www.opengurukul.com 20
21. psql : default database
NOTE :
The default database is postgres.
Example : Default connect to postgres
$ psql
psql (8.4.5)
Type "help" for help.
postgres=# q
$
www.opengurukul.com 21
22. psql : -d : connect to database
Example : Connect to template1
$ psql -dtemplate1
psql (8.4.5)
Type "help" for help.
template1=# q
$
www.opengurukul.com 22
23. psql : s : display history or save
history
Syntax :
s [FILE] display history or save it to file
Example : will list history of commands executed.
postgres-# s
www.opengurukul.com 23
24. psql : i : execute commands from
file
Syntax :
i FILE execute commands from file
Example :
$ cat /tmp/in.sql
l
$
postgres=# i /tmp/in.sql
...will give a list of databases ...
postgres=#
www.opengurukul.com 24
25. psql : o : send all query results to
file
Syntax :
o [FILE] send all query results to file or |pipe
Example :
postgres=# o /tmp/out.txt l
Output :
$ cat /tmp/out.txt
will have list of databases
$
www.opengurukul.com 25
26. psql : ! : execute command in
shell
Syntax :
! [COMMAND] execute command in shell or
start interactive shell
Example :
postgres-# ! sh
sh-4.1$ pwd
/var/lib/pgsql
sh-4.1$ exit
exit
postgres-# www.opengurukul.com 26
27. psql : internal variables
Syntax :
prompt [TEXT] NAME prompt user to set internal
variable
set [NAME [VALUE]] set internal variable, or list all if
no parameters
unset NAME unset (delete) internal variable
www.opengurukul.com 27
28. psql : Display Internal Variables
postgres-# set
AUTOCOMMIT = 'on'
PROMPT1 = '%/%R%# '
PROMPT2 = '%/%R%# '
PROMPT3 = '>> '
VERBOSITY = 'default'
VERSION = 'PostgreSQL 8.4.5 on i386-redhat-linux-gnu, compiled by GCC gcc
(GCC) 4.5.1 20100924 (Red Hat 4.5.1-4), 32-bit'
DBNAME = 'postgres'
USER = 'postgres'
PORT = '5432'
ENCODING = 'UTF8'
www.opengurukul.com 28
postgres-#
29. psql : set variables : Example
postgres-# prompt 'name: ' SITE
name: www.opengurukul.com
postgres-# set
...
SITE = 'www.opengurukul.com'
postgres-#
www.opengurukul.com 29
30. psql : prompt
The psql prompt will be
databasename=# for database super user
databasename=> for users without super user
privileges.
NOTE :
Whichever user has done initdb will have DB
super user privileges or if added otherwise.
www.opengurukul.com 30
32. Introduction : postgres
To start PostgreSQL server, you can directly use
/usr/bin/postgres
$ /usr/bin/postgres -D /var/lib/pgsql/data -p 5432
-D : pgdata directory
-p : port
www.opengurukul.com 32
33. Introduction : postmaster
The postmaster is an alias for PostgreSQL Database Server.
$ which postmaster
/usr/bin/postmaster
$
$ ls -l /usr/bin/postmaster
lrwxrwxrwx. 1 root root 8 Jan 27 2011 /usr/bin/postmaster ->
postgres
$
www.opengurukul.com 33
34. Introduction : Initialize database
cluster : initdb
The initdb command is used to initalize a database
cluster.
A database cluster consists of a data directory that can
have multiple databases served be a postgres
instance.
The initdb creates the directories in which the database
data will live.
The initdb generates the shared catalog tables (tables
that belong to the whole cluster rather than to any
particular database).
The initdb creates two databases :
www.opengurukul.com 34
template1 and postgres
35. Introduction: initdb : template1
Whenever you create a new database in the
cluster, everything in the template1 database is
copied.
We should install only those things in template1
that we would like to copy to other databases.
www.opengurukul.com 35
36. Introduction: initdb : example
Syntax :
$ initdb --pgdata | -D pgdata_directory
Generally, The default pgdata directory is /var/lib/pgsql/data.
If you don't specify any pgdata directory, it will be picked up from
PGDATA environment variable
Example : Create a D/B cluster data1 in home directory
$ initdb -D ~/data1
$
www.opengurukul.com 36
37. Introduction: start d/b cluster
Export PGDATA
$ export PGDATA=~/data1
Modify the port in ~/data1/postgresql.conf to 5433.
$ export PGPORT=5433 # export port
Add user (the one who did initdb) to group “postgres” in /etc/group
file to allow writing to /var/run/postgres/
Start D/B
pg_ctl start
Invoke psql with new port
psql -p <new port> -d postgres
www.opengurukul.com 37
38. Introduction : Create Database :
createdb
The createdb (executable) or
"CREATE DATABASE" (psql
# from psql prompt
command) is used to create
database on PostgreSQL. CREATE DATABASE name;
# from shell command line
createdb name;
www.opengurukul.com 38
39. Introduction : Create Database :
createdb : Example
$ su - postgres
$ createdb x
Example :
postgres=# CREATE DATABASE sample;
CREATE DATABASE
postgres=# l
[List of databases]
Postgres=# q
www.opengurukul.com 39
$
40. Introduction : Delete Database :
dropdb
The dropdb command can be used to delete the
PostgreSQL database.
Syntax :
$ dropdb <dbname>
www.opengurukul.com 40
41. Introduction : Connect to
database
c[onnect] [DBNAME|- USER|- HOST|- PORT|-]
connect to new database (currently "postgres")
Example :
postgres-# c sample
psql (8.4.5)
You are now connected to database "sample".
sample-#
www.opengurukul.com 41
42. Introduction : psql : S, +
Informational
(options: S = show system objects, + = additional detail)
d[S+] list tables, views, and sequences
NOTE: dS+ shows size also
For postgrres, we must specify "S" otherwise objects will not
be shown.
Example :
postgres-# dS
[List of relations]
......
www.opengurukul.com 42
44. Introduction : vacuumdb : garbage-
collect database
The vacuumdb is a utility for cleaning a
PostgreSQL database.
VACUUM reclaims storage occupied by dead
tuples.
In normal PostgreSQL operation, tuples that are
deleted or obsoleted by an update are not
physically removed from their table; they
remain present until a VACUUM is done.
It is necessary to do VACUUM periodically,
especially on frequently-updated tables.
www.opengurukul.com 44
45. Introduction : vacuumdb : options
--full : Perform full vaccuming
--dbname <dbname> : name of the pariculat database
to be cleaned or analyzed
--table table [ (column [,...]) ] : table to be cleaned.
columns can also be specified.
--all : Vacuum all databases
--analyze : Calculate statistics for use by the optimizer
--verbose : verbose display
www.opengurukul.com 45
48. pg_dump : plain dump : script file
The pg_dump command is used to extract a
PostgreSQL database into a .sql script file.
Syntax :
pg_dump database_name > database.sql
www.opengurukul.com 48
49. pg_dump : plain dump : usage
To dump a database called "mydb" into a SQL-
script file:
$ pg_dump mydb > db.sql
To reload such a script into a (freshly created)
database named "newdb".
$ psql -d newdb -f db.sql
www.opengurukul.com 49
50. pg_dump : table
To dump specific tables, specify them after -t
To skip specific tables, specify them after -T
Both -t and -T supports regular expression.
Example :
$ pg_dump -t 'mytab1_*' -T 'mytab2_*' mydb > db.sql
www.opengurukul.com 50
51. pg_dump : archive file
To dump a database into a custom-format archive file:
$ pg_dump -Fc mydb > db.dump
To reload an archive file into a (freshly created)
database named newdb :
$ pg_restore -d newdb db.dump
The default dump format is plain (p) script file that is
specified using -Fp.
www.opengurukul.com 51
53. createuser : create user account
To create a user account (also called role),
createuser command is used.
Only superusers and users with CREATEROLE
privilege can create new users.
You can also use "CREATE ROLE" to achieve it.
www.opengurukul.com 53
54. createuser : create user account :
example
Example :
To create a user matsya1 on the default database
server:
$ createuser matsya1
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases?
(y/n) n
Shall the new role be allowed to create more new roles?
(y/n) n
www.opengurukul.com 54
55. dropuser - remove user account
The dropuser is used to remove an existing
PostgreSQL user.
Only superusers and users with the
CREATEROLE privilege can remove
PostgreSQL users.
You can also use "DROP ROLE <rolename>" to
achieve it.
www.opengurukul.com 55
56. dropuser - remove user account :
example
Example :
To remove user matsya1 from the default database
server:
$ dropuser matsya1
www.opengurukul.com 56
57. list users : du
You can use psql command du to see the users (also called roles).
Example :
List Users / Roles
postgres=> du
List of roles
Role name | Attributes | Member of
-----------+-------------+-----------
postgres | Superuser | {}
: Create role
: Create DB
surikuma | Create role | {}
: Create DB
www.opengurukul.com 57
postgres=>
58. list users : pg_roles
The users (also called roles) are present in pg_roles. We can also
check rolename in pg_roles.
Example :
Users/Roles from pg_roles
postgres=# select rolname from pg_roles;
rolname
----------
postgres
surikuma
(2 rows)
postgres=# www.opengurukul.com 58
59. Role Membership : Grant & Revoke
A group role is created using "CREATE ROLE" command.
This role will also represent a group.
A group role (priviliges) will not have LOGIN attribute generally but
it is not mandatory.
Syntax:
GRANT <group role> to <user role>;
GRANT <group role> from <user role>;
www.opengurukul.com 59
60. Role Membership : Grant & Revoke :
Example
Example :
create a group role and assign priviliges to a user
CREATE ROLE admin NOINHERIT;
CREATE ROLE guru LOGIN INHERIT;
GRANT admin TO guru;
www.opengurukul.com 60
62. createlang : add procedural
language
The createlang is used to add a support of a procedural
language in the database.
Syntax :
$ createlang plpgsql
$ createlang plperl
www.opengurukul.com 62
63. createlang : list procedural
languages
$ createlang --list
Procedural Languages
Name | Trusted?
---------+----------
plpgsql | yes
plperl | yes
$
www.opengurukul.com 63
65. PL/pgSQL : Structure
PL/pgSQL is a block-structured language.
PL/pgSQL Block Structure
[ <<label>> ]
[ DECLARE
declarations ]
BEGIN
statements
END [ label ];
www.opengurukul.com 65
66. PL/pgSQL : Data Types
Frequently used data type are
integer - for numbers
varchar - for variable length character array
char - for fixed length character array
date - for calendar date (year, month, day)
time - for time of day
timestamp - for date & time
The data types table is present on
http://www.postgresql.org/docs/9.1/interactive/datatype.html
www.opengurukul.com 66
67. PL/pgSQL : Variable Declarations
PL/pgSQL variables can Examples :
have any SQL data type,
user_id integer;
such as integer, varchar,
a
and char. quantity numeric(5);
The variable name is url varchar;
specified first and data myrow tablename
type is specified next. %ROWTYPE;
myfield
tablename.columnname
%TYPE;
www.opengurukul.com 67
68. PL/pgSQL : Variable Assignment
In PL/pgSQL the Example :
assignment operator x := 20;
is :=
www.opengurukul.com 68
69. PL/pgSQL : RAISE : Report
Messages
The RAISE can be used to report messages on PL/pgSQL.
Syntax:
RAISE LEVEL 'format' [,expression [, ...]] ;
Possible levels are DEBUG, LOG, INFO, NOTICE, WARNING,
and EXCEPTION.
EXCEPTION raises an error.
Inside the format string, % is replaced by the next optional
argument's string representation
Example :
RAISE NOTICE 'Calling cs_create_job(%)', v_job_id;
www.opengurukul.com 69
70. PL/pgSQL : Create or Replace Function
The CREATE OR REPLACE FUNCTION call is
used to create (replace) a function.
www.opengurukul.com 70
71. PL/pgSQL : Create or Replace Function
: Example
$ cat add.sql $ psql
create or replace function postgres=#i add.sql
add(x integer, y integer) CREATE FUNCTION
returns integer AS $$ postgres=#q
declare $
total integer;
begin
total := x + y +z ;
RETURN total;
end;
$$
language plpgsql; www.opengurukul.com 71
72. PL/pgSQL : List of Functions: df
To get List of Functions use df
Syntax :
df [PATTERN]
Example : Get list of functions using df
postgres=# df
List of functions
+--------+--------+------------------+---------------------------------+--------+
| Schema | Name | Result data type | Argument data types | Type |
+--------+--------+------------------+---------------------------------+--------+
| public | add | integer | x integer, y integer | normal |
+--------+--------+------------------+---------------------------------+--------+
(1 rows) www.opengurukul.com 72
73. PL/pgSQL : Call Function
Use select to call the Example :
function.
postgres=# select
select funcname(funcargs); add(1,2);
sum
-----
3
(1 row)
postgres=#
www.opengurukul.com 73
74. PL/pgSQL : Drop Function
Use DROP FUNCTION to drop a function.
Example :
postgres=# drop function sum(integer, integer);
DROP FUNCTION
postgres=#
NOTE :
The drop function must be called with argument types otherwise it
will not work.
www.opengurukul.com 74
75. PL/pgSQL : Develop Function :
Example 2
$ cat msg.sql
create or replace function msg()
RETURNS INTEGER
AS
$$
begin
RAISE NOTICE 'hello world';
RETURN 0;
end;
$$
language plpgsql; www.opengurukul.com 75
$
76. PL/pgSQL : Create Function : Example
2
postgres=# i msg.sql
CREATE FUNCTION
postgres=#
www.opengurukul.com 76
77. PL/pgSQL : List Function : Example 2
List Function :
postgres=# df msg
List of functions
+-----------+---------+---------------------+-----------------------------+-----------+
| Schema | Name | Result data type | Argument data types | Type |
+-----------+---------+----------------------+-----------------------------+----------+
| public | msg | integer | | normal |
+-----------+---------+----------------------+-----------------------------+-----------+
(1 row)
postgres=#
www.opengurukul.com 77
78. PL/pgSQL : Call Function : Example 2
Call Function :
postgres=# select msg();
NOTICE: hello world
msg
-----
0
(1 row)
postgres=#
www.opengurukul.com 78
79. PL/pgSQL : Drop Function : Example 2
Drop Function :
postgres=# drop function msg();
DROP FUNCTION
postgres=
www.opengurukul.com 79
81. Transactions
Transactions are a fundamental concept of all database
systems.
A transaction bundles multiple steps into a single, all-or-
nothing operation.
The intermediate states between the steps are not
visible to other concurrent transactions.
If some failure occurs that prevents the transaction from
completing, then none of the steps affect the database
at all.
A transaction is said to be atomic: from the point of view
of other transactions, it either happens completely or
not at all. www.opengurukul.com 81
82. Transactions : Savepoint &
Rollback
Create a savepoint
SAVEPOINT [<savepoint_name>];
Rollback to a savepoint
ROLLBACK [to <savepoint_name>];
Commit the changes
COMMIT;
www.opengurukul.com 82
83. Transactions : Savepoint &
Rollback : Example
Take a case of Bank Database, suppose we debit $100.00 fro Ram's account, and credit
Laxman's account, only to find later that we should have credited Sita's account
BEGIN
UPDATE accounts SET balance = balance - 100.00 WHERE name = 'Ram';
SAVEPOINT my_savepoint;
UPDATE accounts SET balance = balance + 100.00 WHERE name = 'Laxman';
-- oops ... forget that and use Sita's account
ROLLBACK TO my_savepoint;
UPDATE accounts SET balance = balance + 100.00 WHERE name = 'Sita';
COMMIT;
END;
www.opengurukul.com 83
85. Thanks
Thanks for attending the session.
For additional support, please post your query on
PostgreSQL discussion forum at
www.opengurukul.com
www.opengurukul.com 85