PostgreSQL教程(4)psql与客户端工具的使用
一、psql基本介绍
psql 是 PostgreSQL 的命令行客户端工具,类似于MySQL中的mysql、Oracle中的sqlplus。通过psql可以实现对 PostgreSQL 数据库的连接和各种管理工作,比如远程登录数据库、 执行 SQL 脚本、实现用户权限管理等。
1、连接PgSQL数据库
psql -h 192.168.0.103 -p 5432 -d tanglu_database -U tanglu #-U:指定用户名 #-W:进行交互式密码验证 #-w:非交互式登录 #-p:指定服务端口 #-h:指定服务地址 #-d:指定连接数据库,默认为postgres #-f:登录时同时指定指定的sql文件,类似mysql -e #连接参数也可以用环境变量指定,这样就不用每次手写参数 export PGDATABASE=tanglu_database export PGHOST=192.168.0.103 export PGPORT=5432 export PGDUSER=postgres
2、查看帮助命令
通过 \? 可以查看所有psql子命令的用法
3、查看当前连接信息
通过 \conninfo 可以查看当前用户与数据库的连接情况,类似操作系统 who 命令
postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/run/postgresql" at port "5432".
4、切换登录数据库
通过 \c 可以切换需要操作的数据库
# 使用\c进行数据库的切换 #如果没有指定库名会直接显示当前连接情况 postgres=# \c You are now connected to database "postgres" as user "postgres". #切换到指定库 postgres=# \c tanglu_database; You are now connected to database "tanglu_database" as user "postgres". #同时切换数据库和用户 postgres=# \c tanglu_database tanglu; You are now connected to database "tanglu_database" as user "tanglu".
5、列出实例中所有数据库
通过 \l 可以切换需要操作的数据库,类似mysql中的show databases,支持通配符过滤,list
tanglu_database=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | tanglu_database | 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)
6、显示当前数据库中的表
通过 \dt 显示当前数据库中的表,display table
tanglu_database=# \dt List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | t1 | table | postgres public | t2 | table | postgres public | t3 | table | postgres
7、查看当前库中所有表的详细信息,如表大小
通过 \d+打印出数据库表的详细信息,包含表大小、属主等
tanglu_database=# \d+ List of relations Schema | Name | Type | Owner | Persistence | Access method | Size | Description --------+------+-------+----------+-------------+---------------+---------+------------- public | t1 | table | postgres | permanent | heap | 0 bytes | public | t2 | table | postgres | permanent | heap | 0 bytes | public | t3 | table | postgres | permanent | heap | 0 bytes | (3 rows)
8、查看具体表结构
通过 \d 打印出指定表的结构
tanglu_database=# \d t1; Table "public.t1" Column | Type | Collation | Nullable | Default --------+-----------------------+-----------+----------+--------- id | integer | | | name | character varying(10) | | |
9、查看索引,display index
通过 \di 打印指定表的索引信息,display index
tanglu_database=# \di
10、查看用户或者角色信息,display user
通过\du 打印用户信息,display user
tanglu_database=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
11、查看PGSQL所支持的参数,类似show variables,同样支持通配符:\df
tanglu_database=# \df *size*
12、导入SQL文件:\i
tanglu_database=# \i test.sql test.sql: No such file or directory
13、导出查询结果到本地文件:\o
tanglu_database=# \o test.sql select * from t1 where id >10; #该查询结果都会存储到test.sql中
14、查看SQL执行时长
tanglu_database=# \timing select * from t1; Time: 202.372ms
15、查看数据库参数配置
postgres=# show $具体参数名 postgres=# show maintenance_work_mem
二、PostgreSQL图形化客户端工具
PostgreSQL官方提供了一款图形化管理工具pgAdmin,官网地址https://www.pgadmin.org/download/,除此之外使用Windows版的PGSQL安装包进行安装时也可以勾选pgAdmin。该工具安装后的使用方式类似SQLyog等工具,有汉化选项
评论