Tuesday, May 15, 2007

Backup and Restore Database.....

There are three approaches to backing up Postgresql database..

1.SQL dump
2.File system level backups
3.On-line backups

1.SQL dump

First log as postgres user..

#su - postgres or su -l postgres

After that you have issue these ..

#pg_dump dname > dump_name

"dname" is the database name,you are going to backup.

"dump_name" is dump name.

2.File system level backups
3.On-line backups

comming soon...

Thursday, May 10, 2007

Alter table - Modify table

Postgresql doesnot provide to modify tables, like Oralcle

To modify table first you have to drop column and then ADD column again..

test2=# ALTER TABLE tbl1 DROP column col1;

test2=# ALTER TABLE tbl1 ADD column col_new VARCHAR(10);

How to create foreign key reference to part of composite key .......

Just say two tables tbl1 and tbl2. tbl1 contains composite key (A,B,C). Then tbl2 contains composite key (A,B). When we want to create foriegn key to table tbl1(A,B) from tbl2(A,B), it gives error....

Because there is not a unique key in tbl1...



sa tt w
sa tt q


sa tt

In that case, there is no unique key on tbl1 that match to tbl2.

solution is, you have to create unique index to that A and B feilds in tbl1 table.



Wednesday, May 9, 2007

create a index in postgreSQL................

Indices are created in an existing table to locate rows more quickly and efficiently. It is possible to create an index on one or more columns of a table, and each index is given a name. The users cannot see the indexes, they are just used to speed up queries.

Updating a table containing indexes takes more time than updating a table without, this is because the indexes also need an update. So, it is a good idea to create indexes only on columns that are often used for a search.

CREATE [ UNIQUE ] INDEX name ON table [ USING method ]
( { column | ( expression ) } [ opclass ] [, ...] )
[ TABLESPACE tablespace ]
[ WHERE predicate ]



Causes the system to check for duplicate values in the table when the index is created (if data already exist) and each time data is added. Attempts to insert or update data which would result in duplicate entries will generate an error.


The name of the index to be created. No schema name can be included here; the index is always created in the same schema as its parent table.


The name (possibly schema-qualified) of the table to be indexed.


The name of the method to be used for the index. Choices are btree, hash, rtree, and gist. The default method is btree.


The name of a column of the table.


An expression based on one or more columns of the table. The expression usually must be written with surrounding parentheses, as shown in the syntax. However, the parentheses may be omitted if the expression has the form of a function call.


The name of an operator class. See below for details.


The tablespace in which to create the index. If not specified, default_tablespace is used, or the database's default tablespace if default_tablespace is an empty string.


The constraint expression for a partial index.

A Unique Index

Creates a unique index on a table. A unique index means that two rows cannot have the same index value.

CREATE UNIQUE INDEX index_name ON table_name (column_name)

The "column_name" specifies the column you want indexed.

A Simple Index

Creates a simple index on a table. When the UNIQUE keyword is omitted, duplicate values are allowed.

CREATE INDEX index_name ON table_name (column_name)

The "column_name" specifies the column you want indexed.


To create a B-tree index on the column title in the table films:

CREATE UNIQUE INDEX title_idx ON films (title);

To create an index on the column code in the table films and have the index reside in the tablespace indexspace:

CREATE INDEX code_idx ON films(code) TABLESPACE indexspace;