Book Image

PostgreSQL Cookbook

By : Chitij Chauhan
Book Image

PostgreSQL Cookbook

By: Chitij Chauhan

Overview of this book

<p>PostgreSQL is an open source database management system. It is used for a wide variety of development practices such as software and web design, as well as for handling large datasets (big data).</p> <p>With the goal of teaching you the skills to master PostgreSQL, the book begins by giving you a glimpse of the unique features of PostgreSQL and how to utilize them to solve real-world problems. With the aid of practical examples, the book will then show you how to create and manage databases. You will learn how to secure PostgreSQL, perform administration and maintenance tasks, implement high availability features, and provide replication. The book will conclude by teaching you how to migrate information from other databases to PostgreSQL.</p>
Table of Contents (19 chapters)
PostgreSQL Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Checking active sessions


In this recipe, we are going to learn how to check for active sessions in a database.

Getting ready

We are going to query the pg_stat_activity table to check for active sessions in a database. The query used in this recipe works in PostgreSQL version 9.2 onwards.

How to do it...

We can use the following SQL query to find the active sessions in the hrdb database:

SELECT pid , usename, application_name, client_addr,
  client_hostname, query, state from pg_stat_activity
where datname='dvdrental';

How it works...

We use the preceding query to find all of the client connections made to the hrdb database. Here is an explanation of the columns in the pg_stat_activity table to find information regarding active sessions in the hrdb database:

  • The pid column: The value in this column indicates the process ID of the currently connected user to the database, the hrdb database in our case.

  • The datname column: The value in this column indicates the name of the database to which the user...