postgresql - 无法创建数据库 - OperationalError:其他用户正在访问源数据库“template1”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14374726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
postgresql - can't create database - OperationalError: source database "template1" is being accessed by other users
提问by Andrius
I logged in to source database template1 and now I can't create database. When I try to create database, I get this error:
我登录到源数据库模板1,现在无法创建数据库。当我尝试创建数据库时,出现此错误:
OperationalError: source database "template1" is being accessed by other users
DETAIL: There are 5 other session(s) using the database.
Every time I login to template1, I use 'exit' command to logout, but as you can see it does not logout and number of sessions increases everytime I login. Is there a way to force disconnect every connection to template1 that logged in now?
每次登录到 template1 时,我都会使用“退出”命令注销,但正如您所看到的那样,每次登录时它都不会注销并且会话数会增加。有没有办法强制断开与现在登录的 template1 的每个连接?
采纳答案by Andrius
This helped me solve my problem:
这帮助我解决了我的问题:
SELECT *, pg_terminate_backend(procpid)
FROM pg_stat_activity
WHERE usename='username';
--Use pid if PostgreSQL version 9.2 or above.
I terminated all active connections to template1 and could create database normally
我终止了与 template1 的所有活动连接,并且可以正常创建数据库
回答by mvp
Database template1
exists only to provide barebone structure to create another empty database. You should neverlogon to template1
, otherwise you will have problems.
数据库的template1
存在只是为了提供准系统结构来创建另一个空数据库。你永远不应该登录template1
,否则你会遇到问题。
Probably easiest solution for you is to restart PostgreSQL server process, and logon again. Database that should always exist and is safe to logon is postgres
.
可能最简单的解决方案是重新启动 PostgreSQL 服务器进程,然后再次登录。应该始终存在并且可以安全登录的数据库是postgres
.
If restarting is not an option, you can use another emergency template database: template0
.
如果重新启动不是一种选择,您可以使用另一个紧急模板数据库:template0
.
By default, this statement:
默认情况下,此语句:
CREATE DATABASE dbname;
is equivalent to:
相当于:
CREATE DATABASE dbname TEMPLATE template1;
If template1
is not available or corrupted, you can use template0
as last resort:
如果template1
不可用或已损坏,您可以将其template0
用作最后的手段:
CREATE DATABASE dbname TEMPLATE template0;
You can read more about template databases here.
您可以在此处阅读有关模板数据库的更多信息。
回答by DASADIYA CHAITANYA
You can also try to terminate the current process thread by the Terminal
您也可以尝试通过终端终止当前进程线程
Search the Process :
搜索过程:
sudo ps aux | grep template1
Kill the Process :
杀死进程:
sudo kill -9 < your process id >
sudo kill -9 <你的进程ID>
回答by roxdurazo
To solve this, I have to disconnect the database connection from the pgAdmin III.
为了解决这个问题,我必须断开与 pgAdmin III 的数据库连接。
回答by Cloud Artisans
I have a script that connects to a database and performs various operations on it, some requiring that no one else is logged in. I modified @Andrius's answer not to terminate my script's connection, but rather terminate everyone else's:
我有一个连接到数据库并对其执行各种操作的脚本,其中一些要求没有其他人登录。我修改了@Andrius 的答案,不是终止我的脚本连接,而是终止其他所有人的连接:
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid();
From the docs: pg_backend_pid()
is the Process ID of the server process attached to the current session.
来自文档:pg_backend_pid()
是附加到当前会话的服务器进程的进程 ID。
回答by Er.Ankit H Gandhi
This problem occur when you had logged(psql template1or psql template0) in template1 and template0 database and exit using below command.
当您在 template1 和 template0 数据库中登录(psql template1或psql template0)并使用以下命令退出时,会发生此问题。
Ctrl + z
Ctrl + z
Better way exist from db use below postgres command then problem will not create:
更好的方法存在于 db use 下面的 postgres 命令然后问题不会产生:
\q + enter
\q + 输入
There are 2 solutions, If have problem.
有2个解决方案,如果有问题。
Solution - 1
解决方案 - 1
Restart posgres service like.
重启 posgres 服务之类的。
sudo service postgresql restart
须藤服务 postgresql 重启
Solution - 2
解决方案 - 2
sudo ps aux | grep template1
须藤ps辅助| grep模板1
Make sure don't delete this processes
确保不要删除此进程
postgres 8363 0.0 0.0 111760 7832 pts/11 T 09:49 0:00 /usr/lib/postgresql/9.5/bin/psql template1 ankit 18119 0.0 0.0 14224 976 pts/14 S+ 12:33 0:00 grep --color=auto template1
postgres 8363 0.0 0.0 111760 7832 pts/11 T 09:49 0:00 /usr/lib/postgresql/9.5/bin/psql template1 ankit 18119 0.0 0.0 14224 976 S pts10:203:203自动模板1
rest of process should be kill using below command.
其余进程应使用以下命令终止。
sudo kill -9
须藤杀 -9
Now try to create db again.
现在尝试再次创建数据库。
Hope this help you.
希望这对你有帮助。
Ankit H Gandhi.
安吉特·H·甘地。
回答by Tomas Kubes
On Windows I had to reinstall PostgreSQL, restart did not help.
在 Windows 上,我不得不重新安装 PostgreSQL,重新启动并没有帮助。
回答by Fioat N
if you go to the side panel , browser, servers, database, then on the right is a summary of the database...becareful here, there is a press right and find the dropdown to delete the database, so if sure, get rid of existing database, and create a new one, so that you can claim ownership of the newly created one
如果你去侧面板,浏览器,服务器,数据库,那么右边是数据库的摘要......这里要小心,有一个按右键并找到删除数据库的下拉列表,所以如果确定,摆脱现有数据库,并创建一个新数据库,以便您可以声明对新创建的数据库的所有权
回答by Anupam Kumar
In windows. shutdown the GUI. do it from psql command prompt.
在窗户里。关闭图形用户界面。从 psql 命令提示符执行此操作。
回答by Mostafa Fallah
I had a same problem but after googling, I understood I have a connection to (postgis_30_sample) database (the template that I want to copy from). This connection was created by GeoServerwhen I created some stores in Geo Server application.
我遇到了同样的问题,但在谷歌搜索之后,我明白我有一个到(postgis_30_sample)数据库(我想从中复制的模板)的连接。当我在 Geo Server 应用程序中创建一些商店时,这个连接是由GeoServer创建的。