签到成功

知道了

CNDBA社区CNDBA社区

PostgreSQL 学习笔记(7) -- PG 数据库 基本操作

2022-12-07 08:53 1480 0 原创 PostgreSQL
作者: dave

在之前的博客,我们了解的PG的相关信息,如下:

PostgreSQL 学习笔记(1) — PG 概述
https://www.cndba.cn/dave/article/116370
PostgreSQL 学习笔记(2) — PG 版本发布策略 和 生命周期说明
https://www.cndba.cn/dave/article/116372
PostgreSQL 学习笔记(3) — PG 单实例安装手册
https://www.cndba.cn/dave/article/116374
PostgreSQL 学习笔记(4) — PG 启动 与 关闭
https://www.cndba.cn/dave/article/116375
PostgreSQL 学习笔记(5) — psql 工具使用说明
https://www.cndba.cn/dave/article/116376
PostgreSQL 学习笔记(6) — PG 参数管理
https://www.cndba.cn/dave/article/116377

本篇我们看下PG的数据库管理。当然我这里的数据库管理是真的对数据库的操作,比如创建、删除、修改数据库。 因为PG的架构和Oracle 不一样,反而有点类似MySQL,在实例初始化后,我们需要创建对应的业务数据库和用户才可以对外使用。

1 创建数据库

完整的创建数据库的语法说明参考官方手册:
https://www.postgresql.org/docs/current/sql-createdatabase.html

语法如下:

https://www.cndba.cn/dave/article/116379

CREATE DATABASE name
    [ WITH ] [ OWNER [=] user_name ]
           [ TEMPLATE [=] template ]
           [ ENCODING [=] encoding ]
           [ STRATEGY [=] strategy ] ]
           [ LOCALE [=] locale ]
           [ LC_COLLATE [=] lc_collate ]
           [ LC_CTYPE [=] lc_ctype ]
           [ ICU_LOCALE [=] icu_locale ]
           [ LOCALE_PROVIDER [=] locale_provider ]
           [ COLLATION_VERSION = collation_version ]
           [ TABLESPACE [=] tablespace_name ]
           [ ALLOW_CONNECTIONS [=] allowconn ]
           [ CONNECTION LIMIT [=] connlimit ]
           [ IS_TEMPLATE [=] istemplate ]
           [ OID [=] oid ]

一般情况下创建数据库,不需要上面那么多的参数,最简单的创建数据库示例如下∶

CREATE DATABASE cndba;

参数说明如下∶

https://www.cndba.cn/dave/article/116379

OWNER [=] user_name∶指定新建的数据库属于哪个用户。如果不指定,新建的数据库就属于当前执行命令的用户。
TEMPLATE [=] template∶模板名(从哪个模板创建新数据库)。如果不指定,将使用默认模板数据库(templatel)。
ENCODING [=] encoding∶创建新数据库使用的字符编码,默认为UTF8。比如GB18030编码创建一个数据库时,代码如下∶
CREATE DATABASE cndba ENCODING ‘GB18030’ TEMPLATE template0;
注意,这里需要指定模板数据库为“template0”,而不能指定为“templatel”,因为编码和区域设置必须与模板数据库的相匹配,如果模板数据库中包含了与新建的数据库之编码不匹配的数据,或者包含了排序受LC_COLLATE和LC_CTYPE 影响的索引,那么复制这些数据将会导致数据库被新设置破坏。template0公认不包含任何会受字符集编码或排序影响的数据或索引,故可以作为创建任意字符集数据库的模板。
TABLESPACE [=] tablespace_name∶指定和新数据库关联的表空间名字。
CONNECTION LIMIT [=] connlimit∶数据库可以接受多少并发的连接。默认为∶-1,表示没有限制。

2 修改数据库

完整的修改数据库的语法说明参考官方手册:
https://www.postgresql.org/docs/current/sql-alterdatabase.htmlhttps://www.cndba.cn/dave/article/116379

语法如下:

ALTER DATABASE name [ [ WITH ] option [ ... ] ]

where option can be:

    ALLOW_CONNECTIONS allowconn
    CONNECTION LIMIT connlimit
    IS_TEMPLATE istemplate

ALTER DATABASE name RENAME TO new_name

ALTER DATABASE name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }

ALTER DATABASE name SET TABLESPACE new_tablespace

ALTER DATABASE name REFRESH COLLATION VERSION

ALTER DATABASE name SET configuration_parameter { TO | = } { value | DEFAULT }
ALTER DATABASE name SET configuration_parameter FROM CURRENT
ALTER DATABASE name RESET configuration_parameter
ALTER DATABASE name RESET ALL

示例如下:

https://www.cndba.cn/dave/article/116379

[dave@www.cndba.cn ~]# su - postgres
Last login: Mon Aug  8 23:52:15 CST 2022 on pts/2
-bash-4.2$
-bash-4.2$ psql
psql (14.6)
Type "help" for help.

postgres=# /l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 cndba     | 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)

postgres=# create database ustc;
CREATE DATABASE
postgres=# /l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 cndba     | 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
 ustc      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(5 rows)


postgres=# alter database ustc connection limit 50;
ALTER DATABASE
postgres=# alter database ustc rename to dave;
ALTER DATABASE
postgres=# alter database dave set enable_indexscan to off;
ALTER DATABASE
postgres=# select current_setting('enable_indexscan');
 current_setting
-----------------
 on
(1 row)

postgres=# /c dave
You are now connected to database "dave" as user "postgres".
dave=#  select current_setting('enable_indexscan');
 current_setting
-----------------
 off
(1 row)

dave=#

3 删除数据库

官网链接如下:
https://www.postgresql.org/docs/current/sql-dropdatabase.htmlhttps://www.cndba.cn/dave/article/116379https://www.cndba.cn/dave/article/116379

DROP DATABASE [ IF EXISTS ] name [ [ WITH ] ( option [, ...] ) ]
where option can be:
FORCE

示例:

https://www.cndba.cn/dave/article/116379

我们重新开了一个窗口,之前的连接没有关闭:

[dave@www.cndba.cn ~]# su - postgres
Last login: Tue Aug  9 09:09:41 CST 2022 on pts/2
-bash-4.2$ psql
psql (14.6)
Type "help" for help.

postgres=# /l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 cndba     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 dave      | 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
(5 rows)

postgres=# drop database dave;
ERROR:  database "dave" is being accessed by other users
DETAIL:  There is 1 other session using the database.
postgres=#

提示提示正在连接的数据库无法删除。

https://www.cndba.cn/dave/article/116379

删除库:

https://www.cndba.cn/dave/article/116379

postgres=# drop database dave;
DROP DATABASE

如果删除不存在的数据库会报错:

postgres=# drop database dave;
ERROR:  database "dave" does not exist

加上if exists 则不会报错了:https://www.cndba.cn/dave/article/116379

postgres=# drop database if exists dave;
NOTICE:  database "dave" does not exist, skipping
DROP DATABASE
postgres=#

版权声明:本文为博主原创文章,未经博主允许不得转载。

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
dave

dave

关注

人的一生应该是这样度过的:当他回首往事的时候,他不会因为虚度年华而悔恨,也不会因为碌碌无为而羞耻;这样,在临死的时候,他就能够说:“我的整个生命和全部精力,都已经献给世界上最壮丽的事业....."

  • 2262
    原创
  • 3
    翻译
  • 578
    转载
  • 192
    评论
  • 访问:8080830次
  • 积分:4349
  • 等级:核心会员
  • 排名:第1名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ