PostgreSQL Common Commands

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

For PostgreSQL database, here are the common commands for your own PostgreSQL

In Windows DOS window connection 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 fields to existing tables:
ALTER TABLE [table name] Add column [Field name] [type];

To delete a field from a table:

ALTER TABLE [table name] Drop column [field name];

Modify Database Column Properties

ALTER TABLE name ALTER COLUMN name Type name (350)

Rename a field:
ALTER TABLE [table name] Rename column [field name a] to [field name B];
* Set a default value for a field:
ALTER TABLE [table name] ALTER column [field name] Set default [new default value];
* Remove default values:
ALTER TABLE [table name] ALTER column [Field name] drop default;
To insert data into a table:
Insert into table name ([Field name m],[field name N],......) VALUES ([column M's Value],[column N's value],......);
To modify the data for a column of a row in a table:
Update [table name] set [Target Field name]=[target value] where [character of the row];
To delete a row of data from a table:
Delete from [table name] where [characteristic of the row];
Delete from [table name];--erase entire table
To create a table:
Create TABLE ([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

Below two points, I think more important:

    1. PostgreSQL is case-sensitive for table names and field names. the graphical interface can be created normally. Use the SQL statement with double quotes, if the JDBC query, etc., remember to use the escape symbol.
    2. PostgreSQL is insensitive to casing in SQL statements such as the Select ID from T_user and the select ID from T_user, which queries the ID from the T_user table. This field. If you want to query the fields in uppercase letters, double quotation marks are also added: select "ID" from T_user

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.