How to list active connections on PostgreSQL?

Cover Image for How to list active connections on PostgreSQL?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to List Active Connections on PostgreSQL

Are you facing the frustrating issue of not being able to drop a database in PostgreSQL because there are active connections to it? Don't worry, we've got you covered! In this article, we'll show you how to list active connections in PostgreSQL and identify the machines they are coming from.

The Problem

You might have encountered a situation where you wanted to drop a database in PostgreSQL using the psql command, only to receive the dreaded error message: "Cannot drop database X because it is being accessed by other users."

This error occurs when there are active connections to the database you want to drop. To proceed, you need to identify those connections and determine which machines they originate from. Let's dive into the solution!

Solution: Using the pg_stat_activity View

In PostgreSQL, you can use the pg_stat_activity system view to get information about the current connections to the database. It provides data such as the process ID, session ID, client IP address, and more.

To list the active connections to a specific database, follow these steps:

  1. Connect to your database using the psql command:

psql -U your_username -d your_database
  1. Once connected, run the following SQL query:

SELECT pid, usename, client_addr, application_name
FROM pg_stat_activity
WHERE datname = 'your_database'
  AND state = 'active';

This query selects the process ID (pid), username (usename), client IP address (client_addr), and application name (application_name) for each active connection to the specified database.

Example Output:

pid  | usename  |   client_addr   |       application_name       
------+----------+-----------------+------------------------------
 1234 | user1    | 192.168.0.100   | MyApp
 5678 | user2    | 192.168.0.101   | MyWebsite

In this example, we can see that there are two active connections to the database "your_database." One comes from IP address 192.168.0.100, associated with the application "MyApp," and the other from IP address 192.168.0.101, associated with the application "MyWebsite."

Taking Action

Now that you have the connection details, you can take appropriate action to resolve the issue. Here are a few approaches you can consider:

  1. Identify the users and request them to disconnect: Contact the users responsible for the active connections and ask them to close their applications or sessions. Once the connections are terminated, you'll be able to drop the database without any issues.

  2. Terminate the connections manually: If you have the necessary privileges, you can terminate the connections yourself. Use the pg_terminate_backend(pid) function to forcibly terminate a connection using its process ID (pid). However, exercise caution when terminating connections, as it may disrupt ongoing operations.

  3. Investigate and diagnose: If you notice persistent connections or connections that should not exist, consider investigating further. Look for potential misconfigurations, long-running queries, or any other anomalies that might be causing the issue.

Conclusion

Listing active connections in PostgreSQL is a crucial step in troubleshooting issues related to dropping databases. By leveraging the pg_stat_activity view, you can easily identify the active connections and take appropriate action.

Remember, terminating connections should be done with caution, as it may disrupt users or ongoing operations. Always communicate with the users responsible for the connections and try to understand the root cause before taking any drastic steps.

Now that you know how to list active connections in PostgreSQL, go ahead and resolve those nagging issues with dropping databases like a pro! 💪

Have you ever encountered this issue? How did you resolve it? Share your experiences and insights in the comments section below. 🗨️


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello