postgresql PGError:错误:其他用户正在访问源数据库“template1”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4977171/
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
PGError: ERROR: source database "template1" is being accessed by other users
提问by holden
I'm having problems getting testing to work with Postgresql and Rails 3.
我在使用 Postgresql 和 Rails 3 进行测试时遇到问题。
Both development and production databases I can get to work fine, however the test database throws the following errors when I run rake
or db:test:prepare
, etc.
开发和生产数据库我都可以正常工作,但是当我运行rake
或db:test:prepare
等时,测试数据库会抛出以下错误。
PGError: ERROR: source database "template1" is being accessed by other users
PGError:错误:其他用户正在访问源数据库“template1”
Update
更新
Googling around, it seems that one should use template0
instead of template1
when using createdb to create a new database in Postgres. In typical “So I'll remove the cause. But not the symptom” fashion, I found vendor/rails/railities/lib/task/databases.rake
and changed line 109 to read:
谷歌搜索,似乎应该使用template0
而不是template1
在使用 createdb 在 Postgres 中创建新数据库时使用。在典型的“所以我会消除原因。但不是症状”时尚,我发现vendor/rails/railities/lib/task/databases.rake
并更改了第 109 行:
createdb #{enc_option} \
-U "#{abcs["test"]["username"]}" \
-T template0 #{abcs["test"]["database"]}
But I don't really wanna do that, as I'm using Rails as a GEM, any one know of another work around or fix?
但我真的不想那样做,因为我使用 Rails 作为 GEM,有人知道另一种解决方法或修复方法吗?
database.yml:
数据库.yml:
development:
adapter: postgresql
encoding: unicode
database: test1234_development
pool: 5
username: holden
password: postgres
test:
adapter: postgresql
encoding: unicode
database: test1234_test
pool: 5
username: holden
password: postgres
Full error:
完整错误:
NOTICE: database "test1234_test" does not exist, skipping
PGError: ERROR: source database "template1" is being accessed by other users
DETAIL: There are 1 other session(s) using the database.
: CREATE DATABASE "test1234_test" ENCODING = 'unicode'
注意:数据库“test1234_test”不存在,跳过
PGError:错误:源数据库“template1”正在被其他用户访问 细节
:还有 1 个其他会话正在使用该数据库。
:创建数据库“test1234_test”编码 = 'unicode'
回答by Mike Sherrill 'Cat Recall'
Short story: CREATE DATABASE
works by copying an existing database. PostgreSQL won't let you copy a database if another session is connected to it. If template1 is being accessed by other users, CREATE DATABASE
will fail.
小故事:CREATE DATABASE
通过复制现有数据库来工作。如果另一个会话连接到它,PostgreSQL 不会让您复制数据库。如果 template1 正在被其他用户访问,CREATE DATABASE
将失败。
The question you need to answer: Why are other sessions connected to template1?
您需要回答的问题:为什么其他会话连接到模板 1?
The difference between template0 and template1
template0和template1的区别
At the point you initialize a database cluster, template0 and template1 are the same. Any location-specific stuff you want to make available to every database you create by using CREATE DATABASE
should go into template1. So, for example, if you add the procedural langauge PL/python to template1, every database you create later will include PL/python.
在初始化数据库集群时,template0 和 template1 是相同的。您希望通过使用创建的每个数据库都可以使用任何特定于位置的内容CREATE DATABASE
都应进入模板 1。因此,例如,如果您将过程语言 PL/python 添加到 template1,您以后创建的每个数据库都将包含 PL/python。
The database template0 is intended to be a "virgin" template. It should contain only standard database objects--the ones created by initializing the cluster. As a "virgin" template, it should never be changed. Never.
数据库模板0 旨在成为“原始”模板。它应该只包含标准数据库对象——通过初始化集群创建的对象。作为一个“处女”模板,它永远不应该被改变。绝不。
If you need to specify encoding and locale settings (collation), then you can do that by copying template0. You can't do that by copying template1.
如果您需要指定编码和区域设置(排序规则),那么您可以通过复制 template0 来实现。您不能通过复制模板 1 来做到这一点。
回答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 pts/10: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 Ajay Takur
Just restart the service of database.
只需重启数据库服务即可。
回答by Ifesinachi Bryan
I restarted my system and the error was still showing. However, I followed the steps below to sort it out.
我重新启动了系统,但错误仍然显示。但是,我按照以下步骤进行了整理。
Stop all processes using the postgres port 5432 by doing this in command prompt (Admin): Type
netstat -ano
in command prompt. Find the pid with Local Address of0.0.0.0:5432
. Then usetaskkill /pid {pid} /f
to kill the task.Start the postgres service in windows services.
通过在命令提示符(管理员)中执行此操作来停止使用 postgres 端口 5432 的所有进程:键入
netstat -ano
命令提示符。找到本地地址为 的pid0.0.0.0:5432
。然后使用taskkill /pid {pid} /f
杀死任务。在windows服务中启动postgres服务。
回答by Adamou BELLO
Solution for me was to delete old server and create a new one from Postgresql administration web interface. Could now create new database without this error.
我的解决方案是删除旧服务器并从 Postgresql 管理 Web 界面创建一个新服务器。现在可以在没有此错误的情况下创建新数据库。
回答by Adamou BELLO
I also got this error while trying to reset the database while I had the default Ruby on Rails server WEBrick running:
我在运行默认的 Ruby on Rails 服务器 WEBrick 时尝试重置数据库时也遇到此错误:
$ bin/rake db:reset
PG::Error: ERROR: database "dev" is being accessed by other users
DETAIL: There is 1 other session using the database.
: DROP DATABASE IF EXISTS "dev"
The other user here was the running Rails app. After shutting down the server withCTRL+ c, I was able to re-run the database reset command without any problems.
这里的另一个用户是正在运行的 Rails 应用程序。使用CTRL+关闭服务器后c,我可以毫无问题地重新运行数据库重置命令。
It makes sense too. You can't drop the database if someone else is currently connected to it, as Mike Sherrill also points out.
这也有道理。如果其他人当前连接到数据库,您不能删除该数据库,正如Mike Sherrill 所指出的那样。