PostgreSQL快速入门:psql工具的使用_MySQL

PHP中文网
发布: 2016-05-27 19:11:40
原创
6475人浏览过

一、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 relations 
 Schema | Name | Type  |  Owner    
--------+------+-------+---------- 
 public | t    | table | postgres
登录后复制

7.\dn命令-列出所有的schema

osdba-# \dn 
        List of schemas 
        Name        |  Owner    
--------------------+---------- 
 information_schema | postgres 
 pg_catalog         | postgres 
 pg_toast_temp_1    | postgres 
 public             | postgres 
(5 rows)
登录后复制

8.\db命令-显示所有表空间

osdba-# \db 
       List of tablespaces 
    Name    |  Owner   | Location  
------------+----------+---------- 
 pg_default | postgres |  
 pg_global  | postgres |  
(2 rows)
登录后复制

9.\dg-列出数据库中所有角色和用户

osdba-# \dg 
            List of roles 
 Role name | Attributes  | Member of  
-----------+-------------+----------- 
 postgres  | Superuser   | {} 
           : Create role    
           : Create DB
登录后复制

10.\dp-显示表的权限分配情况

osdba-# \dp 
                          Access privileges 
 Schema | Name | Type  | Access privileges | Column access privileges  
--------+------+-------+-------------------+-------------------------- 
 public | t    | table |                   |  
(1 row)
登录后复制

四、指定字符集编译的命令

1.\encoding gbk-设置客户端的字符编码为gbk

五、\pset命令

1.\pset命令-用于设置输出的格式,\pset border 0/1/2:输出内容无边框、只有内边框、内外都有边框

 

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)
登录后复制

2.\x命令-可以把表中的每一行的每列数据都拆分为单行展示,如果有一行数据有太多的拆行,显示不下,就可以使用这个命令

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)
登录后复制

五、psql使用技巧

1.使用上下键把过去使用过的命令或者SQL语句调出来,连续两个tab键表示补全或给出提示输入

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
登录后复制

2.psql加上-E参数,可以把psql中各种以"\"开头的命令执行的实际SQL打印出来

-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.
登录后复制

以上就是postgresql快速入门:psql工具的使用_mysql的内容,更多相关内容请关注php中文网(www.php.cn)!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号