mirror of
https://github.com/jbranchaud/til
synced 2026-01-17 14:08:01 +00:00
Add Terminating A Connection as a postgres til
This commit is contained in:
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
|
|||||||
warrant a full blog post. These are mostly things I learn by pairing with
|
warrant a full blog post. These are mostly things I learn by pairing with
|
||||||
smart people at [Hashrocket](http://hashrocket.com/).
|
smart people at [Hashrocket](http://hashrocket.com/).
|
||||||
|
|
||||||
_342 TILs and counting..._
|
_343 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -191,6 +191,7 @@ _342 TILs and counting..._
|
|||||||
- [String Contains Another String](postgres/string-contains-another-string.md)
|
- [String Contains Another String](postgres/string-contains-another-string.md)
|
||||||
- [Temporarily Disable Triggers](postgres/temporarily-disable-triggers.md)
|
- [Temporarily Disable Triggers](postgres/temporarily-disable-triggers.md)
|
||||||
- [Temporary Tables](postgres/temporary-tables.md)
|
- [Temporary Tables](postgres/temporary-tables.md)
|
||||||
|
- [Terminating A Connection](postgres/terminating-a-connection.md)
|
||||||
- [Timestamp Functions](postgres/timestamp-functions.md)
|
- [Timestamp Functions](postgres/timestamp-functions.md)
|
||||||
- [Toggling The Pager In PSQL](postgres/toggling-the-pager-in-psql.md)
|
- [Toggling The Pager In PSQL](postgres/toggling-the-pager-in-psql.md)
|
||||||
- [Truncate All Rows](postgres/truncate-all-rows.md)
|
- [Truncate All Rows](postgres/truncate-all-rows.md)
|
||||||
|
|||||||
42
postgres/terminating-a-connection.md
Normal file
42
postgres/terminating-a-connection.md
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# Terminating A Connection
|
||||||
|
|
||||||
|
Consider the scenario where you are trying to drop a database, but there are
|
||||||
|
existing connections.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ dropdb sample_db
|
||||||
|
dropdb: database removal failed: ERROR: database "sample_db" is being accessed by other users
|
||||||
|
DETAIL: There is 1 other session using the database.
|
||||||
|
```
|
||||||
|
|
||||||
|
If you don't know where these connections are, you can terminate them within
|
||||||
|
a `psql` session. You just have to figure out the `pid` of those
|
||||||
|
connections. In [List Connections To A
|
||||||
|
Database](postgres/list-connections-to-a-database.md), I explained how to
|
||||||
|
get at the `pid` values of connections. Using the `pid` value, you can
|
||||||
|
terminate a connection.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select pg_terminate_backend(12345);
|
||||||
|
pg_terminate_backend
|
||||||
|
----------------------
|
||||||
|
t
|
||||||
|
```
|
||||||
|
|
||||||
|
To terminate all connections to a particular database, use a query like the
|
||||||
|
following:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
select pg_terminate_backend(pg_stat_activity.pid)
|
||||||
|
from pg_stat_activity
|
||||||
|
where pg_stat_activity.datname = 'test_drop'
|
||||||
|
and pid <> pg_backend_pid();
|
||||||
|
pg_terminate_backend
|
||||||
|
----------------------
|
||||||
|
t
|
||||||
|
```
|
||||||
|
|
||||||
|
This excludes the current session, so you'll need to exit `psql` as well
|
||||||
|
before dropping the database.
|
||||||
|
|
||||||
|
[source](http://stackoverflow.com/questions/5408156/how-to-drop-a-postgresql-database-if-there-are-active-connections-to-it)
|
||||||
Reference in New Issue
Block a user