pgsql 如何刪除仍有活動連結的資料庫
阿新 • • 發佈:2021-01-16
刪除資料庫的命令:
drop database if exists testdb;
但是這個命令可能會報錯:
類似於:
database "xxx" is being accessed by other users. DETAIL: There is x other session using database.
如:
表示要刪除的資料庫上還有其他活動連結。
解決方法:
斷開該資料庫上所有連結。
sql:
select pg_terminate_backend(pid) from pg_stat_activity where datname='{0}';
其中:
pg_terminate_backend
pg_stat_activity
是一個系統表,用於儲存服務程序的屬性和狀態。
補充:解決postgresql 資料庫因存在連線刪除不成功的問題
postgresql 資料庫因存在連線刪除不成功問題
#psql -d dbname #SELECT * FROM pg_stat_activity;
找到要刪除資料庫的連線pid
kill -9 pid
重新刪除db
以上為個人經驗,希望能給大家一個參考,也希望大家多多支援我們。如有錯誤或未考慮完全的地方,望不吝賜教。