About postgresql--Common operating instructions

Source: Internet
Author: User
Tags psql

Create a database

CREATE DATABASE Test with OWNER = postgres ENCODING = ' UTF8 ';

Enter the console method to execute the command under the installation purpose bin of PostgreSQL: Psql database name,
Example:/usr/local/pgsql/bin/psql mydb

Specify host, user name, and database, such as:

/usr/local/pgsql/bin/psql-h localhost-u postgres-d Test

View version: Psql--version or SELECT version ();

View all databases: \l

View all databases (including detailed parameters): SELECT * from Pg_database;

Select database: \c databasename

View All tables: \dt

To view the structure of a table: \d tablename

Exit Psql Console: \q

To view the index of a table:

SELECT * from pg_indexes where tablename= ' log ';

To export a backup database:

Pg_dump-h localhost-u postgres databasename >/tmp/databasename.bak.yyyymmdd.sql

Import the Recovery database (the SQL file is the Pg_dump exported file, either the entire database, or just a single table, or just a structure, etc.):

Psql-h localhost-u postgres-d DatabaseName </tmp/databasename.bak.yyyymmdd.sql

Export data structure, mainly with the parameter-s:

Pg_dump-u username-w dbname-f/tmp/filename.sql

To export a table:
Pg_dump-h localhost-u postgres-t tablename dbname > Test.sql

Export the structure of a table, also add parameter "-S":
Pg_dump-h localhost-u postgres-t tablename-s dbname > Test_construct.sql

To export data for a table, add the parameter "-a":
Pg_dump-h localhost-u postgres-t tablename-a dbname > Test_data.sql

View sequence: SELECT * from information_schema.sequences where Sequence_schema = ' public ';

View database size: Select Pg_size_pretty (pg_database_size (' Test '));

View the size of the table: Select Pg_size_pretty (pg_relation_size (' Test '));

About postgresql--Common operating instructions

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.