PostgreSQL Basic Operations

Source: Internet
Author: User
Tags postgresql psql create database import database

Library operations

\h: View an explanation of the SQL command, such as \h Select.
\?: View the list of PSQL commands.
\l: Lists all databases.
\c [database_name]: Connect to a different database.
\d: Lists all tables for the current database.
\d [table_name]: Lists the structure of a table.
\DU: Lists all users.
\e: Opens a text editor.
\conninfo: Lists the current database and connection information.

Log in to Database
Psql-u postgres-w (password)-h 127.0.0.1

Create a database
Create DATABASE Databa_name

Backing Up the database
Pg_dump-h 127.0.0.1-u postgres databasename >/data/backup_name.sql

Import Database
Psql-h localhost-u postgres-d DatabaseName </data/backup_name.sql

To modify a database password
ALTER USER postgres with PASSWORD ' postgres ';

Table Operations

Create a new table
CREATE TABLE user_tbl (name VARCHAR (), signup_date date);

Inserting data
INSERT into USER_TBL (name, signup_date) VALUES (' Zhang San ', ' 2013-12-22 ');

Select Record
SELECT * from USER_TBL;

Update data
UPDATE user_tbl Set name = ' John Doe ' WHERE name = ' Zhang San ';

Deleting records
DELETE from user_tbl WHERE name = ' John Doe ';

Add Field
ALTER TABLE user_tbl ADD email VARCHAR (40);

Update structure
Alter TABLE USER_TBL ALTER COLUMN signup_date SET not NULL;

Rename Field
ALTER TABLE user_tbl RENAME COLUMN signup_date to signup;

Delete Field
ALTER TABLE user_tbl DROP COLUMN email;

Renaming a table
ALTER TABLE user_tbl RENAME to Backup_tbl;

Delete a table
DROP TABLE IF EXISTS backup_tbl;

PostgreSQL Basic Operations

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.