postgresql 隨筆

查看數據庫服務啓動時間。sql

#兩個方法
SELECT pg_postmaster_start_time();
#格式化代碼
SELECT date_trunc('second',current_timestamp - pg_postmaster_start_time()) as "PostgreSQLServiceUpTime";

查看服務已運行時間。數據庫

SELECT current_timestamp - pg_postmaster_start_time() as "ServiceStartTime";
#格式化代碼
SELECT date_trunc('second',current_timestamp - pg_postmaster_start_time()) as "UpTime";

查看當前數據庫存儲空間佔用狀況。session

SELECT pg_database_size(current_database());

查看錶行數。架構

#根據實際狀況修改表名
SELECT count(*) FROM "public"."CN";

查詢數據庫擴展模塊信息。app

SELECT * FROM pg_extension;

本文測試環境輸出結果以下。socket

postgres=# SELECT * FROM pg_extension;
 extname | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition
---------+----------+--------------+----------------+------------+-----------+--------------
 plpgsql |       10 |           11 | f              | 1.0        |           |
(1 row)

postgres=#

使用'\x'選項有以下相似結果輸出。ide

postgres=# SELECT * FROM pg_extension;
-[ RECORD 1 ]--+--------
extname        | plpgsql
extowner       | 10
extnamespace   | 11
extrelocatable | f
extversion     | 1.0
extconfig      |
extcondition   |

更改會話參數。例如:post

set work_mem = '32MB';

部分代碼和結果。測試

postgres=# set work_mem = '32MB';
SET
postgres=# set local work_mem = '16MB';
WARNING:  SET LOCAL can only be used in transaction blocks
SET
postgres=# reset all;
RESET
postgres=#
postgres=#
postgres=# set work_mem = '32MB';
SET
postgres=#
postgres=# select name,setting,reset_val,source from pg_settings where source ='session';
-[ RECORD 1 ]-------
name      | work_mem
setting   | 32768
reset_val | 4096
source    | session

postgres=#

查看配置文件。spa

postgres=# show config_file;
               config_file
------------------------------------------
 /etc/postgresql/9.5/main/postgresql.conf
(1 row)

postgres=# \x
Expanded display is on.
postgres=# show config_file;
-[ RECORD 1 ]-----------------------------------------
config_file | /etc/postgresql/9.5/main/postgresql.conf

postgres=#

該方法還能夠查詢hba_file和ident_file等配置文件信息。

列出全部參數。

SELECT name,source,setting FROM pg_settings order by 2,1;

結果太長不予列出。例如:

postgres=# SELECT name,source,setting FROM pg_settings where source !='default' and source !='ove
rride' order by 2,1;
            name            |        source        |                 setting
----------------------------+----------------------+------------------------------------------
 application_name           | client               | psql
 client_encoding            | client               | UTF8
 DateStyle                  | configuration file   | ISO, MDY
 default_text_search_config | configuration file   | pg_catalog.english
 dynamic_shared_memory_type | configuration file   | posix
 external_pid_file          | configuration file   | /var/run/postgresql/9.5-main.pid
 lc_messages                | configuration file   | en_US.UTF-8
 lc_monetary                | configuration file   | en_US.UTF-8
 lc_numeric                 | configuration file   | en_US.UTF-8
 lc_time                    | configuration file   | en_US.UTF-8
 log_line_prefix            | configuration file   | %t [%p-%l] %q%u@%d
 log_timezone               | configuration file   | PRC
 max_connections            | configuration file   | 100
 port                       | configuration file   | 5432
 shared_buffers             | configuration file   | 16384
 ssl                        | configuration file   | on
 ssl_cert_file              | configuration file   | /etc/ssl/certs/ssl-cert-snakeoil.pem
 ssl_key_file               | configuration file   | /etc/ssl/private/ssl-cert-snakeoil.key
 stats_temp_directory       | configuration file   | /var/run/postgresql/9.5-main.pg_stat_tmp
 TimeZone                   | configuration file   | PRC
 unix_socket_directories    | configuration file   | /var/run/postgresql
 max_stack_depth            | environment variable | 2048
(22 rows)

postgres=#

PostgreSQL擴展或模塊能夠經過手動下載源代碼編譯和在PGXN(PostgreSQL擴展網,網址爲 http://pgxn.org/ )安裝,也能夠經過系統自帶軟件管理器下載安裝。

管理員權限帳號從新加載配置文件。

postgres=# select pg_reload_conf();
 pg_reload_conf
----------------
 t
(1 row)

postgres=#

配置用戶鏈接數。本文測試用戶是test,請以本機實際狀況爲準。修改鏈接數不影響現有鏈接。

#-1爲解除限制,該值能夠超過數據庫最大鏈接數,取決於實際須要
ALTER ROLE test CONNECTION LIMIT 1;

建立表和查看錶結構信息練習。

test=# create table student (
test(# studentid int primary key,
test(# studentname varchar(30),
test(# studentage int
test(# );
CREATE TABLE
test=# \d
                關聯列表
 架構模式 |  名稱   |  類型  |  擁有者
----------+---------+--------+----------
 public   | student | 數據表 | postgres
(1 行記錄)

test=#
test=#
test=# \d student
           數據表 "public.student"
    欄位     |         類型          | 修飾詞
-------------+-----------------------+--------
 studentid   | integer               | 非空
 studentname | character varying(30) |
 studentage  | integer               |
索引:
    "student_pkey" PRIMARY KEY, btree (studentid)

test=#

test=# \d
                關聯列表
 架構模式 |  名稱   |  類型  |  擁有者
----------+---------+--------+----------
 public   | student | 數據表 | postgres
(1 行記錄)

test=#
test=#
test=# \d student
           數據表 "public.student"
    欄位     |         類型          | 修飾詞
-------------+-----------------------+--------
 studentid   | integer               | 非空
 studentname | character varying(30) |
 studentage  | integer               |
索引:
    "student_pkey" PRIMARY KEY, btree (studentid)

test=#
test=# insert into student values(1,'顧留芳',7),(2,'林業平',6),(3,'徐長卿',7);
INSERT 0 3
test=#
test=# select studentid,studentname,studentage from student;
 studentid | studentname | studentage
-----------+-------------+------------
         1 | 顧留芳       |          7
         2 | 林業平       |          6
         3 | 徐長卿       |          7
(3 行記錄)

test=#

#批量更新數據
test=# update student set studentage =6;
UPDATE 3
test=#
test=# select studentid,studentname,studentage from student;
 studentid | studentname | studentage
-----------+-------------+------------
         1 | 顧留芳       |          6
         2 | 林業平       |          6
         3 | 徐長卿       |          6
(3 行記錄)

test=#
相關文章
相關標籤/搜索