Original address: http://blog.chinaunix.net/uid-26642180-id-3485465.html
I've been learning PostgreSQL recently, and here are some of the common commands that I've compiled for PostgreSQL.
Connect to the database, the default user and database is Postgres
Psql-u user-d dbname
switch databases, equivalent to MySQL's use dbname
\c dbname
Enumerate the databases, equivalent to the MySQL show databases
\l
List table, equivalent to the MySQL show tables
\dt
View table structure, equivalent to desc tblname,show columns from Tbname
\d Tblname
\di View Index
To create a database:
Create DATABASE [DB name];
To delete a database:
drop database [DB name];
* Rename a table:
ALTER TABLE [table name a] rename to [table name B];
* Delete a table:
drop table [table name];
* Add field to existing table:  
ALTER TABLE [table name] Add column [Field name] [type];
* Delete field in table:  
ALTER TABLE [table name] Drop column [field name];
* Rename a field:  
ALTER TABLE [table name] Rename column [field name a] to [field name b];
* Set default value for a field:
ALTER TABLE [table name] ALTER column [field name] Set default [new default value];
* Remove default value:  
ALTER TABLE [table name] ALTER column [Field name] Drop default;
Insert data in table:
insert into table name ([Field name M] , [field name N],......) VALUES ([Column M's Value],[column N's value],......);
modifies data for a column of a row in a table:
Update [table name] set [Target Field name]=[target value] where [the row feature];
Delete a row of data from a table:
Delete from [table name] where [the row feature];
Delete from [table name];--delete entire table
CREATE table:
Create tables ([field name 1] [Type 1] ;,[field Name 2] [ Type 2],...... <,primary key (field name m, field name N,...) >;);
\copyright display of PostgreSQL usage and release terms
\encoding [character encoding name]
Display or set the user-side character encoding
\h [name] SQL command syntax description, with * Show all commands
\prompt [Text] Name
Prompt user to set internal variables
\password [USERNAME]
Securely change the password for a user
\q Exit Psql
Can be done using pg_dump and Pg_dumpall. For example, back up the sales database:
Pg_dump Drupal>/opt/postgresql/backup/1.bak
PostgreSQL Common Commands