如何解决CREATE DATABASE 报错 ”source database "template1" is being accessed by other users“ 问题

数据库关系型数据库技术服务知识库
问题描述

在使用 CREATE DATABASE 语句时,报错 "ERROR: source database "template1" is being accessed by other users"

问题分析

根据官网描述可以知道,这是 PostgreSQL 的预期行为,当从 template1 复制数据的时候,如果存在连接的话,就会失败报错

The principal limitation is that no other sessions can be connected to the source database while it is being copied. CREATE DATABASE will fail if any other connection exists when it starts;

解决方案

分别创建两个连接会话,分别执行不同的命令复现报错问题 会话1:连接到 template1 模板数据库中

postgres=# \c template1;
You are now connected to database "template1" as user "postgres".

会话2:执行 CREATE DATABASE 语句

postgres=# create database tmp_database1;
ERROR:  source database "template1" is being accessed by other users
DETAIL:  There is 1 other session using the database.

因为会话 1 中的连接导致 CREATE DATABASE 语句失败,所以我们退出会话 1 中的连接,然后重新执行相同的语句,可以正常创建数据。

postgres=# create database tmp_database1;
CREATE DATABASE
参考文档

https://www.postgresql.org/docs/12/manage-ag-templatedbs.html 如果您有其他问题,欢迎您联系火山引擎技术支持服务

24
0
0
0
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论