Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force client disconnect using PostgreSQL

Tags:

postgresql

Is there a way to force clients to disconnect from PostgreSQL? I'm looking for the equivlent of DB2's force application all.

I'd like to do this on my development box because when I've got database consoles open, I can't load a database dump. I have to quit them first.

like image 243
Luke Francl Avatar asked Apr 07 '09 19:04

Luke Francl


People also ask

How do I terminate a connection in PostgreSQL?

How to cancel PostgreSQL queries. In PostgreSQL there are two functions we need to take into consideration when talking about cancellation or termination: pg_cancel_backend(pid) : Terminate a query but keep the connection alive. pg_terminate_backend(pid) : Terminate a query and kill the connection.

How do I force a database to drop?

A new command-line option is added to dropdb command, and a similar SQL option “FORCE” is also added in DROP DATABASE. Using the option -f or –force with dropdb command or FORCE with DROP DATABASE to drop the database, it will terminate all existing connections with the database.

How do I stop a running query in PostgreSQL?

If you want to terminate all running queries, the following statement can be executed: SELECT pg_cancel_backend(pid) FROM pg_stat_activity WHERE state = 'active' and pid <> pg_backend_pid(); The above statement will kill all active queries and should only be used in special situations.

What is connection timeout in PostgreSQL?

connectTimeout = int. The timeout value used for socket connect operations. If connecting to the server takes longer than this value, the connection is broken. The timeout is specified in seconds and a value of zero means that it's disabled.


1 Answers

Kills idle processes in PostgreSQL 8.4:

SELECT procpid, (SELECT pg_terminate_backend(procpid)) as killed from pg_stat_activity    WHERE current_query LIKE '<IDLE>'; 
like image 104
bonyiii Avatar answered Sep 27 '22 20:09

bonyiii