一、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)
?
a.安装好后,默认会有一个叫postgres的数据库,还有两个模板数据库template0和template1;
b.用户再建数据库的时候,默认是从模板数据库template1克隆出来;
c.template0是一个最简化的模板库,创建数据库时,如果明确指定从此数据库集成,将创建一个最简化的数据库;
?
2.创建数据库osdba
?
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)
?
3.访问osdba数据库
-bash-4.1$ psql osdba
psql (8.4.20)
Type "help" for help.
?
4.在数据库osdba中创建表t
?
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
?
5.查看osdb数据库中的表
?
osdba=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | t | table | postgres
(1 row)
?
6.创建数据库testdb后,并连接到testdb数据库
?
osdba=# CREATE DATABASE testdb;
CREATE DATABASE
osdba=# \c testdb
psql (8.4.20)
You are now connected to database "testdb".
testdb=#
?
三、psql的常用命令
1.\d命令-查看当前数据库中的所有表
?
osdba-# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | t | table | postgres
(1 row)
?
2.\d命令-跟一个表命,查看这个表的结构定义
?
osdba-# \d t
Table "public.t"
Column | Type | Modifiers
--------+-----------------------+-----------
id | integer | not null
name | character varying(40) |
Indexes:
"t_pkey" PRIMARY KEY, btree (id)
?
3.\d命令-可以查看表格t的索引信息
?
osdba-# \d t_pkey
Index "public.t_pkey"
Column | Type
--------+---------
id | integer
primary key, btree, for table "public.t"
?
4.\d命令-跟通配符如*或?
?
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, btree, for table "public.t"
?
5.\d+命令,显示比\d命令更加详细的信息,显示与表列关联的注释
?
osdba-# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+------+-------+----------+---------+-------------
public | t | table | postgres | 0 bytes |
(1 row)
?
6.匹配不同对象类型的\d命令,如\dt-只想显示匹配的表;\di-只想显示索引;\ds-只显示序列;\dv-只显示视图;\df-只显示函数等...
?
osdba-# \dt t*
List of relati