Ralfs
PostgreSQL
Commands
# install
yum -y install postgresql
service postgresql start
# or
/etc/rc.d/init.d/postgresql start
# usage
psql template1
pg_dump dbname > outfile
# Restoring the dump
psql dbname < infile
pg_dumpall
# Restoring the dump
psql -f infile postgres
[root@box ~]# su - postgres
-bash-3.1$ psql template1
postgres=# \q
Rights
kinds of privilege:
SELECT, INSERT, UPDATE, DELETE, RULE, REFERENCES, TRIGGER,
CREATE, TEMPORARY, EXECUTE, and USAGE.
GRANT { { SELECT | INSERT | UPDATE | DELETE | RULE | REFERENCES | TRIGGER }
[,...] | ALL [ PRIVILEGES ] }
ON [ TABLE ] tablename [, ...]
TO { username | GROUP groupname | PUBLIC } [, ...] [ WITH GRANT OPTION ]
...
GRANT role [, ...] TO username [, ...] [ WITH ADMIN OPTION ]
###
REVOKE [ GRANT OPTION FOR ]
{ { SELECT | INSERT | UPDATE | DELETE | RULE | REFERENCES | TRIGGER }
[,...] | ALL [ PRIVILEGES ] }
ON [ TABLE ] tablename [, ...]
FROM { username | GROUP groupname | PUBLIC } [, ...]
[ CASCADE | RESTRICT ]
###
CREATE ROLE name [ [ WITH ] option [ ... ] ]
CREATE USER name;
createuser [option...] [username]
# createuser is a wrapper around the SQL command CREATE ROLE.
### Examples
CREATE ROLE joe LOGIN INHERIT;
CREATE ROLE admin NOINHERIT;
GRANT admin TO joe;
# joe have use of privileges granted directly to joe plus any privileges
# granted to admin, because joe "inherits" admin's privileges.
SET ROLE wheel;
GRANT UPDATE ON accounts TO joe;
REVOKE ALL ON accounts FROM PUBLIC;
GRANT ALL ON accounts TO PUBLIC;
ALTER ROLE davide WITH PASSWORD 'hu8jmn3';
CREATE USER user1 WITH PASSWORD 'secret' CREATEDB VALID UNTIL '2007-12-31';
CREATE GROUP grname WITH USER u1, u2;
ALTER GROUP grname ADD USER u3;
GRANT privilege ON object TO {PUBLIC | GROUP grame | username }
[ WITH GRANT OPTION ] ;
### look what is:
SELECT * FROM pg_roles;
The psql program's \du meta-command is also useful for listing the existing roles.
SELECT * FROM pg_user;
SELECT * FROM pg_shadow;
/var/lib/pgsql/data/pg_hba.conf
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# user name that ident reports for the connection (typically the Unix user name)
host postgres all 127.0.0.1/32 ident sameuser
# IPv6 local connections:
host all all ::1/128 ident sameuser
# remote connections
host user1 all 192.168.93.0/24 md5
Important places:
/var/lib/pgsql/data/pg_log
/var/lib/pgsql/data
SQL commands
Important SQL commands:
SELECT * FROM pg_database;
SELECT * FROM pg_tables;
SELECT * FROM pg_tables WHERE schemaname='public';
SELECT * FROM pg_namespace;
SELECT * FROM pg_tablespace;
CREATE DATABASE mydb WITH ENCODING 'UNICODE';
Links