PostgreSQL Common Commands

Source: Internet
Author: User
Tags mysql show databases mysql show tables psql

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];

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] <references Associated table name (associated field name) >;,[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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.