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=#
相关文章
相关标签/搜索