一、psql介绍
psql是Postgresql中的一个命令行交互式客户端工具,类似Oracle中的命令行工具sqlplus:1.允许你交互地键入sql或命令,然后把它们发出给Postgresql服务器,再显示sql或命令的结果;2.输入的内容还可以来自一个文件;3.还提供了一些元命令和多种类似shell的特性来实现书写脚本,以及对对量任务的自动化工作;二、psql的简单实用按照前面的步骤,切换su - postgres用户,实用psql工具连接数据库。1.查看有哪些数据库
postgres=# \l
List of databases
Name | Owner | Encoding | Collation | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres : postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres : postgres=CTc/postgres
(3 rows)
postgres=# CREATE DATABASE osdba; CREATE DATABASE postgres-# \l List of databases Name | Owner | Encoding | Collation | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- osdba | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres : postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres : postgres=CTc/postgres (4 rows)
osdba=# create table t(id int primary key,name varchar(40)); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t_pkey" for table "t" CREATE TABLE
osdba=# \d List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | t | table | postgres (1 row)
osdba=# CREATE DATABASE testdb; CREATE DATABASE osdba=# \c testdb psql (8.4.20) You are now connected to database "testdb". testdb=#
osdba-# \d List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | t | table | postgres (1 row)
osdba-# \d t Table "public.t" Column | Type | Modifiers --------+-----------------------+----------- id | integer | not null name | character varying(40) | Indexes: "t_pkey" PRIMARY KEY,btree (id)
osdba-# \d t_pkey Index "public.t_pkey" Column | Type --------+--------- id | integer primary key,btree,for table "public.t"
osdba-# \d t* Table "public.t" Column | Type | Modifiers --------+-----------------------+----------- id | integer | not null name | character varying(40) | Indexes: "t_pkey" PRIMARY KEY,btree (id) Index "public.t_pkey" Column | Type --------+--------- id | integer primary key,sans-serif; line-height: 15px;">5.\d+命令,显示比\d命令更加详细的信息,显示与表列关联的注释
osdba-# \d+ List of relations Schema | Name | Type | Owner | Size | Description --------+------+-------+----------+---------+------------- public | t | table | postgres | 0 bytes | (1 row)
osdba-# \dt t* List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | t | table | postgres
osdba-# \dn List of schemas Name | Owner --------------------+---------- information_schema | postgres pg_catalog | postgres pg_toast_temp_1 | postgres public | postgres (5 rows)
osdba-# \db List of tablespaces Name | Owner | Location ------------+----------+---------- pg_default | postgres | pg_global | postgres | (2 rows)
osdba-# \dg List of roles Role name | Attributes | Member of -----------+-------------+----------- postgres | Superuser | {} : Create role : Create DB10.\dp-显示表的权限分配情况
osdba-# \dp Access privileges Schema | Name | Type | Access privileges | Column access privileges --------+------+-------+-------------------+-------------------------- public | t | table | | (1 row)
osdba-# \pset border 0 Border style is 0. osdba-# \dp Access privileges Schema Name Type Access privileges Column access privileges ------ ---- ----- ----------------- ------------------------ public t table (1 row) osdba-# \pset border 1 Border style is 1. osdba-# \dp Access privileges Schema | Name | Type | Access privileges | Column access privileges --------+------+-------+-------------------+-------------------------- public | t | table | | (1 row) osdba-# \pset border 2 Border style is 2. osdba-# \dp Access privileges +--------+------+-------+-------------------+--------------------------+ | Schema | Name | Type | Access privileges | Column access privileges | +--------+------+-------+-------------------+--------------------------+ | public | t | table | | | +--------+------+-------+-------------------+--------------------------+ (1 row)
osdba-# \x Expanded display is on. osdba-# \dp Access privileges +-[ RECORD 1 ]-------------+--------+ | Schema | public | | Name | t | | Type | table | | Access privileges | | | Column access privileges | | +--------------------------+--------+ osdba-# \x Expanded display is off. osdba-# \dp Access privileges +--------+------+-------+-------------------+--------------------------+ | Schema | Name | Type | Access privileges | Column access privileges | +--------+------+-------+-------------------+--------------------------+ | public | t | table | | | +--------+------+-------+-------------------+--------------------------+ (1 row)
osdba-# \d \d \dc \dD \dew \dFd \dg \dn \ds \dT \da \dC \des \df \dFp \di \do \dS \du \db \dd \deu \dF \dFt \dl \dp \dt \dv
-bash-4.1$ psql -E postgres psql (8.4.20) Type "help" for help. postgres=# \d ********* QUERY ********** SELECT n.nspname as "Schema",c.relname as "Name",CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' END as "Type",pg_catalog.pg_get_userbyid(c.relowner) as "Owner" FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','v','S','') AND n.nspname <> 'pg_catalog' AND n.nspname <> 'information_schema' AND n.nspname !~ '^pg_toast' AND pg_catalog.pg_table_is_visible(c.oid) ORDER BY 1,2; ************************** No relations found
如果你在使用之后,想立即关闭
postgres=# \set ECHO_HIDDEN off
postgres=# \d
No relations found.