0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PostgreSQLでユーザ(Role)あたりのコネクション数を制限したい

Posted at

はじめに

PostgreSQLでユーザ(Role)あたりのコネクション数を制限したいときは、ALTER ROLE ~ CONNECTION LIMIT N; を使いましょう。

(PostgresqlはユーザもRoleとして表現されます)

公式ドキュメントの記載箇所

サンプル

ユーザは shase-san というのを使っています。

ALTER ROLEの発行

ALTER ROLE shase-san CONNECTION LIMIT 5;

確認

foodb=> \du shase-san
                                 List of roles
  Role name   |  Attributes   |                    Member of
--------------+---------------+-------------------------------------------------
 shase-san    | 5 connections | {}

実際にpsqlコマンドで試した所、上記の場合5接続しかできなくなっています。

おまけ

特定ユーザの現在のコネクションをすべてkill

SELECT
  pg_terminate_backend(pid)
FROM
  pg_stat_activity
WHERE
  usename = 'shase-san'
AND
  pid <> pg_backend_pid();

おわりに

PostgreSQL の達人に教えてもらいました。感謝 :pray:

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?