PostGreSQL

Create Super-User

create user <username> createdb createuser;

Create/Modify User Password

alter user <username> with password '<password>';

Modify a column type (PostGreSQL 8)

ALTER TABLE <table> ALTER COLUMN <column> TYPE <new_type>;

Modify a column type (the old way)

BEGIN;
ALTER TABLE <table> ADD COLUMN <new_column> <new_type>;
UPDATE <table> SET <new_column> = CAST(<old_column> AS <new_type>);
ALTER TABLE <table> DROP COLUMN <old_column>;
COMMIT;

Add an UNIQUE constraint

ALTER TABLE <table> ADD CONSTRAINT table_column_key UNIQUE (<column>);

Drop a constraint

ALTER TABLE <table> DROP CONSTRAINT <constraint>;

Log SQL queries

In postgresql.conf:

log_statement = 'all'

View Current SQL Queries

In postgresql.conf:

stats_command_string = true

Then look at table pg_stat_activity

String Concatenation in SQL queries

Example:

UPDATE region SET code='0'||id;

Get next sequence value (update sequence as well)

Example:

SELECT NEXTVAL('hibernate_sequence');

PostGreSQL on Ubuntu (Synaptic installation)

Folder

/etc/postgresql/8.1

Logs

tail /var/log/postgresql/postgresql-8.1-main.log

Allow external hosts

In /etc/postgresql/8.1/main/pg_hba.conf:

host    all         all         206.12.30.0    255.255.255.0      md5

In /etc/postgresql/8.1/main/postgresql.conf:

listen_addresses = '*'
 
write a message
Name


City


Email (won't be displayed)




Antispam: enter the current year (2008)