postgresql基本命令操作

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介:

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 | usebypassrls |  passwd  | valuntil | useconfig 
----------+----------+-------------+----------+---------+--------------+----------+----------+-----------
 postgres |       10 | t           | t        | t       | t            | ******** |          | 
(1 row)

testdb02=#  select * from pg_user ;
 usename  | usesysid | usecreatedb | usesuper | userepl | usebypassrls |  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 | 
 usebypassrls | boolean | 
 passwd       | text    | 
 valuntil     | abstime | 
 useconfig    | text[]  |



 本文转自 wjw555 51CTO博客,原文链接:http://blog.51cto.com/wujianwei/1970402

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
9月前
|
关系型数据库 Linux 数据库
PostgreSQL 入门指南:安装、配置与基本命令
本文从零开始,详细介绍如何在 Windows、Linux 和 macOS 上安装和配置 PostgreSQL,涵盖30+个实操代码示例。内容包括安装步骤、配置远程访问和用户权限、基础数据库操作命令(如创建表、插入和查询数据),以及常见问题的解决方案。通过学习,你将掌握 PostgreSQL 的基本使用方法,并为后续深入学习打下坚实基础。
10447 1
|
关系型数据库 数据库 PostgreSQL
postgresql|【基于pg_basebackup命令的归档备份和恢复---热备冷恢复方式】
postgresql|【基于pg_basebackup命令的归档备份和恢复---热备冷恢复方式】
809 0
|
SQL 关系型数据库 数据库
PostgreSQL常用命令,启动连接,pg_dump导入导出
PostgreSQL常用命令,启动连接,pg_dump导入导出
|
SQL 关系型数据库 PostgreSQL
PostgreSQL和greenplum的copy命令可以添加字段吗?
【6月更文挑战第5天】PostgreSQL和greenplum的copy命令可以添加字段吗?
255 3
|
监控 关系型数据库 数据库
PostgreSQL和greenplum的copy命令如何使用?
【6月更文挑战第5天】PostgreSQL和greenplum的copy命令如何使用?
560 2
|
SQL 关系型数据库 数据库
Postgresql基本操作命令
这些是PostgreSQL数据库的一些基本操作命令,用于创建、管理和查询数据库。根据您的需求,可以使用这些命令执行各种数据库操作。
796 4
|
SQL 关系型数据库 MySQL
MySQL【实践 02】MySQL迁移到PostgreSQL数据库的语法调整说明及脚本分享(通过bat命令修改mapper文件内的SQL语法)
MySQL【实践 02】MySQL迁移到PostgreSQL数据库的语法调整说明及脚本分享(通过bat命令修改mapper文件内的SQL语法)
553 0
|
关系型数据库 数据库 数据安全/隐私保护
postgresql |数据库 |postgresql数据库的短命令详细介绍
postgresql |数据库 |postgresql数据库的短命令详细介绍
160 0
|
存储 SQL 关系型数据库
postgresql常见命令及操作
  pgsql已经更新到beta11了,不同版本的服务器启动或相关命令、配置可能会有不同,所以得根据pg版本进行操作。下面记录一些工作中常用到的一些操作,主要包括服务启动、备份/恢复数据、数据目录迁移、常见操作命令 本文环境: postgres : v10.3 os: MAC 虽然已经在kong部署中介绍了postgres的部署,为了行文连贯性,这里再简单记录下pg的启动相关命令。
4597 0
|
SQL 安全 前端开发
PostgreSQL 高权限命令执行 (CVE-2019-9193)漏洞复现&实战
PostgreSQL 高权限命令执行 (CVE-2019-9193)漏洞复现&实战

推荐镜像

更多