Remember to delete the PostgresSql database once and report an error: There are N other sessions using the database solution

1. Database construction

1, yum specified directory installation

https://blog.csdn.net/llwy1428/article/details/105143053

2, yum install directly

https://blog.csdn.net/llwy1428/article/details/102486414

3. Compile and install

https://blog.csdn.net/llwy1428/article/details/95444151

4. PostgreSql basic operation

https://blog.csdn.net/llwy1428/article/details/102598732

2. Problems encountered

In PostgreSQL 9.2 and above, execute the following statement:

postgres=# drop database dbtest;
# 执行删除指定数据库的时候,报以下错误
ERROR:  database "dbtest" is being accessed by other users
DETAIL:  There is 2 other session using the database.

Or use a third-party database connection tool such as Navicat to report the following error when deleting the specified database:

It means that two clients are connecting to this database at this time, and the database cannot be deleted at this time.

If you are sure to delete this database forcibly, execute the following command:

 

postgres=# SELECT pg_terminate_backend(pg_stat_activity.pid)
postgres-# FROM pg_stat_activity
postgres-# WHERE datname='dbtest' AND pid<>pg_backend_pid();
 pg_terminate_backend 
----------------------
 t
 t
 t
(3 rows)

After executing the above statement, after executing the DROP operation, you can delete the database.
The statement above explains:

pg_terminate_backend: A function used to terminate the connection with the database process id.

pg_stat_activity: is a system table used to store the attributes and status of the service process.

pg_backend_pid(): is a system function to obtain the ID of the server process attached to the current session.

 

At this time, perform the operation of deleting the database:

postgres=# drop database dbtest;
DROP DATABASE

Successfully deleted the database forcibly!

 

At this point, deleting the database reports an error: The problem of N other sessions using the database has been successfully resolved!

 

Expand

1. Yum installs and configures PgAdmin4

https://blog.csdn.net/llwy1428/article/details/102486511

2. Database installation extension

https://blog.csdn.net/llwy1428/article/details/105167524

Guess you like

Origin blog.csdn.net/llwy1428/article/details/103681734