1. 程式人生 > >Postgres限制每個使用者只能連線指定數量的session,防止伺服器資源緊張

Postgres限制每個使用者只能連線指定數量的session,防止伺服器資源緊張

限制每個使用者只能連線指定數量的session,防止伺服器資源緊張

(1)建立測試使用者test:

highgo=#create user test;

CREATEROLE

highgo=#\du

                             List of roles

 Role name |                   Attributes                   | Member of

-----------+------------------------------------------------+----------

 highgo   | Superuser, Create role, Create DB, Replication | {}

 test     |                                               | {}

(2)設定僅允許使用者test使用一個連線

highgo=#ALTER ROLE test CONNECTION LIMIT 1;

ALTERROLE

(3)在session 1中使用test使用者連線highgo資料庫

highgo=>\c highgo  test

Youare now connected to database "highgo" as user "test".

highgo=>

(4)在session 2中也使用test使用者連線highgo資料庫,會出現如下錯誤:

highgo=#\c highgo  test

致命錯誤:  由角色"test"發起的連線太多了

Previousconnection kept

(5)查詢使用者test連結限制

highgo=>SELECT rolconnlimit FROM pg_roles WHERE rolname = 'test';

 rolconnlimit

--------------

            1

(1row)