【5】PostgreSQL 数据库管理(前序)默认库/模板库

数据库管理(前序)

前序篇,主要是对 PostgreSQL 默认的几个数据库进行说明、介绍下每个库的用途等等。后续章节在使用这些默认库时,不在逐一说明了。

默认库

首先,当数据库安装完成之后,执行\l(L字母的小写)可以看到数据库清单,如下:


-- 如下 \l 其后是没有加号(+)
postgres=# \l
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres

postgres=# 


-- 注意:这里执行的命令有一个加号(+),可以看到更多的一些信息.
postgres=# \l+
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |                       | 7941 kB | pg_default | default administrative connection database
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +| 7737 kB | pg_default | unmodifiable empty database
           |          |          |             |             | postgres=CTc/postgres |         |            | 
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +| 7925 kB | pg_default | default template for new databases
           |          |          |             |             | postgres=CTc/postgres |         |            | 

postgres=# 

以下是 \l+ 命令:每一列的含义解释:

  1. Name:数据库的名称。
  2. Owner:数据库所有者的名称。
  3. Encoding:数据库的字符编码。
  4. Collate:数据库的排序规则(Collation)。
  5. Ctype:数据库的字符分类(Character Classification)。
  6. Access privileges:对数据库的访问权限。
  7. Size:数据库占用的磁盘空间大小。
  8. Tablespace:表空间(Tablespace)的名称。
  9. Description:数据库的描述信息。

在输出的结果中,每一行表示一个数据库,每一列对应于数据库的不同属性。
例如,第一行表示 postgres 数据库,它的所有者是 postgres,使用的字符编码是 UTF8,排序规则和字符分类也是 zh_CN.UTF-8,占用的磁盘空间为 7941 kB,没有指定表空间,且具有默认的访问权限等。

从上述的查询结果中,可以看出:安装完成后会默认创建以下几个数据库:

  1. postgres:这是默认的管理数据库,用于管理 PostgreSQL 服务器和用户角色。它包含了用于管理服务器和用户的系统表,例如 pg_rolespg_database 等。通常不建议用于存储实际数据,而是用于管理目的。

  2. template0:这是一个空模板数据库,用于创建其他数据库的模板。它作为创建新数据库的模板,但是不允许对其进行更改,以确保其始终是一个空数据库。

  3. template1:与 template0 类似,也是一个模板数据库,用于创建其他数据库的模板。但与 template0 不同的是,template1 是可以被修改的,可以在其中创建用户自定义的对象,例如函数、表等。通常情况下,我们可以在 template1 中创建通用的配置、用户、函数等,以便在创建新数据库时能够从 template1 中继承这些配置。

模板库

在上述的 默认库 中,提到了 管理数据库模板数据库template0template1),
管理数据库 很好理解:就是存放了一些数据库系统层面的视图(pg_roles等等)的库,用于管理数据库,类似 MySQL 中的 sys、information_schema 等这类库。

那么,什么是模板数据库??

再说 模板数据库 之前,先看如下一个示例:

  1. 分别切换至 template0template1 ,查看其内,是否有表?
    => 查看结果:无其他表.
    => 注意:切换至 template0 时,切换失败了。(后续会说明原因为什么不能切)
-- 切换失败了(暂时先跳过,尝试切换 template1 )
postgres=# \c template0
FATAL:  database "template0" is not currently accepting connections
Previous connection kept
postgres=# 

-- 切换 至 template1 中
postgres=# \c template1
You are now connected to database "template1" as user "postgres".

-- 查看发现:没有表
template1=# \d
Did not find any relations.
template1=# 

  1. 创建数据库 testdb01 ,并查看期内有哪些表?
-- 创建 testdb01 数据库
postgres=# create database testdb01;
CREATE DATABASE
postgres=# 

-- 查看有哪些表?
-- => 查看结果:无表.
template1=# \c testdb01;
You are now connected to database "testdb01" as user "postgres".
testdb01=# 
testdb01=# \d
Did not find any relations.
testdb01=# 
  1. 再次切换到 template1 库中,创建 client 表(这张表自定义测试用的)
-- 切换到 template1 库
postgres=# \c template1
You are now connected to database "template1" as user "postgres".
template1=# 

-- 创建 client 表
template1=# CREATE TABLE client (
template1(#     client_id SERIAL PRIMARY KEY,
template1(#     client_name VARCHAR(100),
template1(#     contact_name VARCHAR(100),
template1(#     email VARCHAR(100),
template1(#     phone VARCHAR(20)
template1(# );
CREATE TABLE
template1=# 
  1. 创建 testdb02 数据库,并查看期内是否有表?
-- 当前数据库 template1 ,查看其内 client 表已经创建成功.
template1=# \d
 public | client               | table    | postgres
 public | client_client_id_seq | sequence | postgres

-- 创建 testdb02 库
template1=# create database testdb02;
CREATE DATABASE

-- 切换至 testdb02 库
template1=# \c testdb02;
You are now connected to database "testdb02" as user "postgres".

-- 查看 testdb02 库,发现:其内也有  client 表.
testdb02=# \d
 public | client               | table    | postgres
 public | client_client_id_seq | sequence | postgres

testdb02=# 

猜测 :创建的数据库和 template0template1 存在某种关系;现 template1 发生改变(创建 client 表),那之前创建的 testdb01 库中,是否也有 client 表?或者在创建一个新的 testdb03 库,是否会有 client 表?

检测猜想

-- 切回到 testdb01 库,查看是否有 client 表?
-- => 发现:无表.
testdb02=# \c testdb01
You are now connected to database "testdb01" as user "postgres".
testdb01=# 
testdb01=# \d
Did not find any relations.
testdb01=# 

-- 创建新的数据库 testdb03
testdb01=# create database testdb03;
CREATE DATABASE

-- 切换到新库 testdb03,检查是否有 client 表?
-- => 发现:有 client 表.
testdb01=# \c testdb03
You are now connected to database "testdb03" as user "postgres".
testdb03=# 
testdb03=# \d
 public | client               | table    | postgres
 public | client_client_id_seq | sequence | postgres

testdb03=#

那在猜测一下:若将 template1 中的 client 表删除,再次创建数据库 testdb04 ,其内是否会有 client 表??
自行测试一下哈~

通过上面的示例,不难看出,创建数据库时,实际上是通过复制一个已有数据库进行的。在默认情况下,将复制 template1 这个数据库,换言之:此数据库(template1)是创建其他新数据库的一个模板

那回到最初的问题:什么是模板数据库?? 这个问题也就迎刃而解了。

补充说明:如果给 template1 增加一些对象(例如:添加表),那么这些对象将被复制到后续创建的新库中。

Q
在上述展示的默认库中,除了 template1 模板库之外,还有一个 template0 ,它又是弄啥嘞?(它又是做什么用的?)、和 template1 有什么区别?

A
template0template1 是两个特殊的模板数据库,用于创建新数据库时作为模板。它们之间的区别在于:

  1. template0

    • template0 是一个严格意义上的空模板数据库,其中不包含任何用户自定义的对象。它只包含了必要的系统表和系统视图,用于创建新数据库时提供一个纯净的模板。
    • 由于 template0 是一个纯净的模板,用户不能在其中创建任何对象或修改其中的内容。即使是超级用户也不能在 template0 中进行修改。
  2. template1

    • template1 也是一个模板数据库,但它允许用户在其中创建用户自定义的对象,例如表、函数、视图等。当创建新数据库时,默认情况下会从 template1 复制数据库结构和内容。
    • template1 的内容可以由用户自行修改,例如可以在其中创建通用的配置、用户、函数等对象,以便在创建新数据库时能够从 template1 中继承这些配置。

总的来说,template0 是一个纯净的、只读的空模板数据库,而 template1 是一个可修改的模板数据库,用户可以在其中创建自定义对象。通常情况下,建议不要修改 template0,而是在 template1 中进行用户自定义的操作。

除了上述的区别之外,你可能会举手在这里插入图片描述发言:补充另外一个区别template0 不能切换,template1 可以切换。
其实,这个算不上区别(个人理解)。因为是否可以切换、是“进行了属性设置的”。

现进行说明下:
Q 为什么不能切换至template0库,而可以切换至 template1 库?
A 执行如下 SQL,查看数据库一些属性信息(datistemplate, datallowconn).

postgres=# select oid, datname, datistemplate, datallowconn from pg_database;

 13580 | postgres  | f             | t
     1 | template1 | t             | t
 13579 | template0 | t             | f --看到这个属性了吧!是 False。。。人家 template1的属性是 True.
 24603 | testdb01  | f             | t
 24613 | testdb02  | f             | t
 24614 | testdb03  | f             | t

postgres=# 

部分属性列的含义:

  1. datistemplate

    • 这个列用于标识数据库是否是一个模板数据库(template database)。
    • 如果 datistemplate 的值为 true,则表示该数据库是一个模板数据库,可以作为其他数据库的模板来创建。
    • 如果 datistemplate 的值为 false,则表示该数据库不是模板数据库,不能用作其他数据库的模板。
  2. datallowconn

    • 这个列用于标识数据库是否允许客户端连接(allow connections)。
    • 如果 datallowconn 的值为 true,则表示该数据库允许客户端连接,可以对其进行查询和操作。
    • 如果 datallowconn 的值为 false则表示该数据库不允许客户端连接,通常是一些系统数据库或模板数据库。
### JMS与MySQL的集成或交互 在现代企业级应用开发中,Java消息服务(JMS)经常用于实现异步通信模式,而MySQL作为广泛使用的开源关系型数据库管理系统,在许多应用场景下负责数据持久化。为了满足复杂的业务需求,常常需要将这两种技术结合起来使用。 #### 使用场景分析 当应用程序接收到通过JMS传递的消息时,可能需要执行一系列操作来处理这些信息并将最终结果保存到MySQL数据库中。例如,在订单处理系统里,前端Web服务器可以向后台发送包含新订单详情的消息;中间件接收该消息后解析其内容,并调用相应的接口把有关记录写入到MySQL中的订单表内[^1]。 #### 实现方式探讨 一种常见的做法是在监听器类内部编写逻辑代码片段,每当有新的消息到达指定队列时触发特定方法运行: ```java @MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"), @ActivationConfigProperty(propertyName="destination", propertyValue="queue/orders") }) public class OrderProcessor implements MessageListener { private static final Logger logger = LoggerFactory.getLogger(OrderProcessor.class); @Resource(name="jdbc/MySqlDS") DataSource dataSource; public void onMessage(Message msg){ try{ TextMessage tm=(TextMessage)msg; String orderDetails=tm.getText(); // Process the incoming message and extract necessary data Connection conn=dataSource.getConnection(); PreparedStatement pstmt=null; try{ pstmt=conn.prepareStatement("INSERT INTO orders (details) VALUES (?)"); pstmt.setString(1,orderDetails); int affectedRows=pstmt.executeUpdate(); if(affectedRows>0){ logger.info("Order successfully processed."); } }finally{ if(pstmt!=null)pstmt.close(); if(conn!=null)conn.close(); } }catch(JMSException e){ throw new RuntimeException(e); } catch (SQLException ex) { throw new RuntimeException(ex); } } } ``` 上述例子展示了如何利用MDB(Message Driven Bean)机制捕获来自某个预定义目标的消息流,进而完成从读取消息体直至更新本地存储整个流程的操作[^4]。 #### 数据一致性保障措施 考虑到网络延迟等因素可能导致事务失败的情况发生,建议采用XA两阶段提交协议或其他补偿策略确保跨资源间的同步性。此外还可以考虑引入消息确认机制以及设置合理的超时参数等手段提高系统的稳定性和可靠性。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值