postgresql基本命令操作:

登陆数据库:

[postgres@localhost ~]$ psql -Utestwjw -h 127.0.0.1 -dpostgres -p 36985

Password for user testwjw:

psql.bin (9.5.9)

Type "help" for help.

postgres=>

切换数据库:

postgres=> \c testdb1

You are now connected to database "testdb1" as user "testwjw".

查看所有的数据库:

testdb1=> \l

testdb1=> \list

List of databases

Name    |  Owner   | Encoding |   Collate   |    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

testdb1   | testwjw  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/testwjw          +

|          |          |             |             | testwjw=CTc/testwjw

testdb2   | testwjw  | UTF8     | en_US.UTF-8 | en_US.UTF-8 |

(5 rows)

查看所有的表:

testdb1=> \dt

List of relations

Schema | Name  | Type  |  Owner

--------+-------+-------+---------

public | t     | table | testwjw

public | t1    | table | testwjw

public | tlb01 | table | testwjw

(3 rows)

testdb1=>

创建数据库:

[postgres@localhost ~]$ psql -p 36985

psql.bin (9.5.9)

Type "help" for help.

postgres=# create database testdb3 with encoding='utf8' owner=testwjw;

CREATE DATABASE

[postgres@localhost ~]$ createdb testdb5  -p 36985

[postgres@localhost ~]$ createdb testdb6  -p 36985

查看创建的数据库:

[postgres@localhost ~]$ psql -p 36985 -c '\list'|egrep "testdb4|testdb5"

testdb4   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |

testdb5   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |

删除创建的数据库:

#以testwjw的身份连接服务器,删除testdb1数据库。

[postgres@localhost ~]$ dropdb -Utestwjw -p 36985 -e testdb1

DROP DATABASE testdb1;

[postgres@localhost ~]$ psql -p 36985 -c '\list'|grep "testdb1"

通过查看系统表验证该数据库是否已经被删除:

[postgres@localhost ~]$ psql -p 36985 -c "SELECT count(*) FROM pg_database WHERE datname ='testdb1'"

count

-------

0

(1 row)

证明此数据库确实被删除。

查看数据库中所有的表以及单表结构:

testdb2=# \dt

List of relations

Schema | Name | Type  |  Owner

--------+------+-------+---------

public | tlb2 | table | testwjw

(1 row)

testdb2=# \d tlb2

Table "public.tlb2"

Column |         Type          | Modifiers

--------+-----------------------+-----------

id     | integer               |

pay    | character varying(20) |

name   | character varying(6)  |

Indexes:

"uniq" UNIQUE CONSTRAINT, btree (id)

testdb2=#

查看索引详细信息:

testdb2=# \d uniq;

Index "public.uniq"

Column |  Type   | Definition

--------+---------+------------

id     | integer | id

unique, btree, for table "public.tlb2"

\d+ 命令:将会显示比\d命令更详细的信息,除了前面介绍的那些,它还会显示任何与表列相关的注释,以及表中出现的OID。

testdb2=# \d+

List of relations

Schema | Name | Type  |  Owner  |  Size   | Description

--------+------+-------+---------+---------+-------------

public | tlb2 | table | testwjw | 0 bytes |

(1 row)

testdb2=# \d

List of relations

Schema | Name | Type  |  Owner

--------+------+-------+---------

public | tlb2 | table | testwjw

(1 row)

testdb2=#

列出所有的schemas:

testdb2=# \dn

List of schemas

Name  |  Owner

--------+----------

public | postgres

(1 row)

创建schema:

testdb2=# create schema sa;

CREATE SCHEMA

testdb2=# \dn

List of schemas

Name  |  Owner

--------+----------

public | postgres

sa     | postgres

(2 rows)

testdb2=#

显示sql执行的时间,可以使用\timing参数:

testdb2=# \timing

Timing is on.

testdb2=# select * from tlb2;

id | pay | name

----+-----+------

(0 rows)

Time: 0.177 ms

testdb2=#

如果想列出数据库中所有的角色或者用户,可以使用\du   \dg,这两个命令等价,因为postgresSQL中用户和角色不区分:

testdb2=# \du

List of roles

Role name |                         Attributes                         | Member of

-----------+------------------------------------------------------------+-----------

postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

testwjw   |                                                            | {}

testdb2=# \dg

List of roles

Role name |                         Attributes                         | Member of

-----------+------------------------------------------------------------+-----------

postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

testwjw   |                                                            | {}

testdb2=#

查看表字段:

testdb2=# SELECT a.attname from pg_class c,pg_attribute a,pg_type t where c.relname='tlb2' and a.attnum>0 and a.attrelid=c.oid and a.atttypid=t.oid;

attname

---------

id

pay

name

(3 rows)

Time: 0.586 ms

testdb2=# \dnp+

List of schemas

Name  |  Owner   |  Access privileges   |      Description

--------+----------+----------------------+------------------------

public | postgres | postgres=UC/postgres+| standard public schema

|          | =UC/postgres         |

sa     | postgres |                      |

(2 rows)

testdb2=# \dn

List of schemas

Name  |  Owner

--------+----------

public | postgres

sa     | postgres

(2 rows)

testdb2=#

创建表:

testdb2=# \dt

List of relations

Schema | Name | Type  |  Owner

--------+------+-------+---------

public | tlb2 | table | testwjw

(1 row)

建表:

testdb2=# CREATE TABLE products (

product_no integer,

name text,

price numeric

);

CREATE TABLE

查看表:

testdb2=# \dt

List of relations

Schema |   Name   | Type  |  Owner

--------+----------+-------+----------

public | products | table | postgres

public | tlb2     | table | testwjw

(2 rows)

删除表:

testdb2=# drop table products;

DROP TABLE

testdb2=# \dt

List of relations

Schema | Name | Type  |  Owner

--------+------+-------+---------

public | tlb2 | table | testwjw

(1 row)

testdb2=#

列出所有的表空间:

postgres=# \db

List of tablespaces

Name      |  Owner   |         Location

---------------+----------+--------------------------

my_tablespace | postgres | /data/postgresql/mydata

pg_default    | postgres |

pg_global     | postgres |

tbspace01     | postgres | /data/postgresql/tbspace

(4 rows)

查看当前用户:

testdb02=# select user;
 current_user
--------------
 postgres
(1 row)

查看当前时间:

testdb02=# select now();
              now              
-------------------------------
 2017-10-30 04:06:49.657883+08
(1 row)

查看当前数据库版本:

testdb02=# select version();
                                                 version                                                  
----------------------------------------------------------------------------------------------------------
 PostgreSQL 9.5.9 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-55), 64-bit
(1 row)

查看数据库用户:

testdb02=#  select * from pg_user where usename='postgres';
 usename  | usesysid | usecreatedb | usesuper | userepl | usebypa***ls |  passwd  | valuntil | useconfig
----------+----------+-------------+----------+---------+--------------+----------+----------+-----------
 postgres |       10 | t           | t        | t       | t            | ******** |          |
(1 row)

testdb02=#  select * from pg_user ;
 usename  | usesysid | usecreatedb | usesuper | userepl | usebypa***ls |  passwd  | valuntil | useconfig
----------+----------+-------------+----------+---------+--------------+----------+----------+-----------
 postgres |       10 | t           | t        | t       | t            | ******** |          |
 replica  |    16384 | f           | f        | t       | f            | ******** |          |

查看pg_user 表结构:

postgres-# \d pg_user;
     View "pg_catalog.pg_user"
    Column    |  Type   | Modifiers
--------------+---------+-----------
 usename      | name    |
 usesysid     | oid     |
 usecreatedb  | boolean |
 usesuper     | boolean |
 userepl      | boolean |
 usebypa***ls | boolean |
 passwd       | text    |
 valuntil     | abstime |
 useconfig    | text[]  |

转载于:https://blog.51cto.com/wujianwei/1970402

postgresql基本命令操作相关推荐

  1. 计算机网络基本操作命令的使用,计算机网络-路由器基本命令操作实验指导书--华为...

    计算机网络-路由器基本命令操作实验指导书--华为 (9页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 9.9 积分 计算机网络实验指导书路由器基本命令操作 ...

  2. freecplus框架-PostgreSQL数据库操作

    文章目录 一.源代码说明 二.概述 三.connection类 四.sqlstatement类 五.程序流程 1.无结果集SQL的程序的流程 2.有结果集SQL的程序的流程 六.示例程序 1.创建超女 ...

  3. Docker 镜像基本命令操作

    目录标题 Docker 镜像基本命令操作 一.Docker 安装 二.镜像操作 Docker 镜像基本命令操作 一.Docker 安装 Docker要求运行在Centos 7上,要求系统为64位,系统 ...

  4. MySQL基本命令操作

    一.MySQL基本命令操作 1.MySql服务开启关闭指令 服务关闭:net stop MySQL 服务关闭:net start MySQL 登陆:mysql -h localhost -P 3306 ...

  5. redis-----07-----redigo基本命令操作(主要讲如何让go的struct、map展开成redis的参数,以及使用struct获取redis返回的key-value批量数组)

    1 请求回应模式 redis 与 client 之间采用请求回应模式,一个请求包对应一个回应包.但是也有例外,pub/sub 模式下,client 发送 subscribe 命令并收到回应包后,之后被 ...

  6. postgresql数据库操作

    postgresql数据库操作 1.数据库基本操作 创建数据库 查看所有数据库 进入数据库 删除数据库 退出数据库 2.数据库修改操作 修改数据库名称 1.数据库基本操作 创建数据库 create d ...

  7. postgresql 数据库操作点记

    postgresql 数据库操作点记 普通查询 查询结果拼接 查询所有字段 排除某些字段 处理时间 查询结果去除null值 安装扩展 自动填充uuid 数据库插入guid函数 清空表格数据 分组查询的 ...

  8. Windows基本命令操作、网络相关操作、windows用户管理、NTFS权限、更新策略的方法

    一.Windows基本命令操作 一.目录与文件应用操作 1.cd 命令 用于改变当前提示符盘符路径或提示符目录路径 CD [/D] [drive:][path] #跨盘符切换必须要跟上 /d 选项 举 ...

  9. 计算机网络华为路由器配置实验,计算机网络 路由器基本命令操作实验报告格式 华为.doc...

    计算机网络路由器基本命令操作实验报告格式华为整理 昆明理工大学信息工程与自动化学院学生实验报告 ( 20 -20 学年 第 学期 ) 课程名称:计算机网络 开课实验室: 201 年 月 日 年级.专业 ...

最新文章

  1. 使用脚本完成AutoCAD自动化任务课程
  2. 如何在同一系统里同时启动多个Tomcat
  3. 短 URL 服务,怎么设计与实现?
  4. 集成算法中的Bagging
  5. 场效应与三极管 电路标识符_看懂这6个提示,轻松搞定恒流源电源电路设计!...
  6. sqlservcer行列互转
  7. 线程池与Callable更配哦
  8. Telnet三步完成连接(简短版)
  9. java string hash变量_java基础(六)-----String性质深入解析
  10. pythondraw解释_科学网—Draw figures with Python - 高琳琳的博文
  11. Java学习日记之 Java-IO流
  12. (继承及其访问限定符)(派生类及其默认成员函数)(赋值兼容规则)
  13. 硬件文章远程视频监控
  14. 学校老师要求微信群里的家长下载钉钉建群,解散微信群,钉钉是不正当商业竞争吗?
  15. 手把手教你做一个Excel 2007数据透视表(有图有真相)
  16. Bus Hound实用教程
  17. 单调栈和单调队列的本质区别
  18. 2020-02-08
  19. 数字先锋 | 铺设一条县域医疗“康庄大道”!
  20. Smt贴片加工出现元件立碑的解决方法

热门文章

  1. 熊猫分发_熊猫下降列和行
  2. 学好C++能够从事哪些岗位?
  3. ngx_lua 模块提供的指令和API等
  4. form 表单序列化 serialize
  5. 设计模式解密(9)- 装饰者模式
  6. shell中if条件字符串、数字比对,` `和[ ]区别
  7. Android第三方开源水面波浪波形view:WaveView(电量、能量、容量指示)
  8. Visual studio 2013 添加 GitHub
  9. 【Pandas】CSV文件读取时手动指定头部
  10. TCP连接吞吐率和线路效率的总结