|
/Admin/databases/PostgreSQL:
PostgreSQL Cheat Sheet
/etc/postgresql/9.0/main/pg_hba.conf specifies the users that can access PostgreSQL. In Debian, the postgres user is the default. Therefore, to create a database[2]:
sudo su
su postgres
createdb dbname
Create a user and grant privileges on the new database:
su postgres
createuser uname
psql dbname
GRANT ALL PRIVILEGES ON DATABASE dbname TO uname;
To create a database and user with SQL:
su postgres
psql
CREATE DATABASE dbname;
CREATE USER uname WITH PASSWORD 'pswd';
GRANT ALL PRIVILEGES ON DATABASE dbname TO uname;
To dump and then restore a database[1]:
pg_dump dbname > outfile
psql dbname < infile
Simple SQL:
psql
\c dbname # connect to a database (similar to "use dbname;" in MySQL)
\dt # list all tables in database
\d tname # show table structure / columns of table tname
SELECT * FROM tname; # list contents of table tname
SELECT * FROM tname WHERE colname='string'; # select on column contents exactly
SELECT * FROM tname WHERE colname ~ 'string*'; # select using regular expression
UPDATE tname SET colname='newstring' WHERE colname='oldstring';
[1] http://www.postgresql.org/docs/9.0/interactive/backup-dump.html
[2] http://www.yolinux.com/TUTORIALS/LinuxTutorialPostgreSQL.html
posted at: 07:18 | path: /Admin/databases/PostgreSQL | permanent link to this entry