= PostgreSQL = [[PageOutline]] * psql 指令說明 - https://www.postgresql.org/docs/current/static/app-psql.html == 修改管理者密碼 == {{{ ~$ sudo su - postgres ~$ psql postgres=# \password postgres Enter new password: Enter it again: postgres=# \q }}} == 資料庫列表 == {{{ ~$ sudo su - postgres ~$ psql postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | zh_TW.UTF-8 | zh_TW.UTF-8 | template0 | postgres | UTF8 | zh_TW.UTF-8 | zh_TW.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | zh_TW.UTF-8 | zh_TW.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows) }}} == 新增使用者 == {{{ ~$ sudo su - postgres -bash-4.2$ createuser 使用者名稱 }}} == 新增資料庫 == {{{ ~$ sudo su - postgres -bash-4.2$ createdb 資料庫名稱 }}} == 允許使用者可以管理指定資料庫 == {{{ ~$ sudo su - postgres -bash-4.2$ psql -d postgres ### 使用 postgres 資料庫 postgres=# ALTER USER 使用者名稱 WITH ENCRYPTED PASSWORD '密碼'; postgres=# GRANT ALL PRIVILEDGES ON DATABASE 資料庫名稱 TO 使用者名稱; }}} == 新增使用者為管理者 == {{{ ~$ sudo su - postgres -bash-4.2$ psql -d postgres ### 使用 postgres 資料庫 postgres=# ALTER USER 使用者名稱 WITH SUPERUSER; }}} == 查詢目前連線數 == * 查連線總數 {{{ postgres=# SELECT count(*) FROM pg_stat_activity; count ------- 52 (1 row) }}} * 依資料庫查詢各別連線數 {{{ postgres=# SELECT datname, count(*) FROM pg_stat_activity GROUP BY datname; }}} == 修改同時連線數上限 == * 修改 postgresql.conf {{{ $ sudo grep max_connections /var/lib/postgresql/postgresql.conf max_connections = 300 # (change requires restart) # Note: Increasing max_connections costs ~400 bytes of shared memory per # max_locks_per_transaction * (max_connections + max_prepared_transactions) }}} == 查詢同時連線數上限 == * 使用 `SHOW` 指令 {{{ postgres=# SHOW max_connections; }}} * 或從 `pg_settings` 資料表查詢 {{{ postgres=# SELECT * FROM pg_settings WHERE name = 'max_connections'; }}} == 效能調校 == * https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server * [工具] https://github.com/jfcoz/postgresqltuner * 相依 `DBD::Pg` 與 `DBI` 兩個 Perl 模組 {{{ # Missing Perl module 'DBD::Pg'. Please install it yum install -y perl-DBD-Pg # Missing Perl module 'DBI'. Please install it yum install -y perl-DBI # Please install theses Perl modules }}}