postgresql 随笔
2019独角兽企业重金招聘Python工程师标准>>>
查看数据库服务启动时间。
#两个方法
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";
查看当前数据库存储空间占用情况。
SELECT pg_database_size(current_database());
查看表行数。
#根据实际情况修改表名
SELECT count(*) FROM "public"."CN";
查询数据库扩展模块信息。
SELECT * FROM pg_extension;
本文测试环境输出结果如下。
postgres=# SELECT * FROM pg_extension;extname | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition
---------+----------+--------------+----------------+------------+-----------+--------------plpgsql | 10 | 11 | f | 1.0 | |
(1 row)postgres=#
使用'\x'选项有如下类似结果输出。
postgres=# SELECT * FROM pg_extension;
-[ RECORD 1 ]--+--------
extname | plpgsql
extowner | 10
extnamespace | 11
extrelocatable | f
extversion | 1.0
extconfig |
extcondition |
更改会话参数。例如:
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 | sessionpostgres=#
查看配置文件。
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.confpostgres=#
该方法还可以查询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 | psqlclient_encoding | client | UTF8DateStyle | configuration file | ISO, MDYdefault_text_search_config | configuration file | pg_catalog.englishdynamic_shared_memory_type | configuration file | posixexternal_pid_file | configuration file | /var/run/postgresql/9.5-main.pidlc_messages | configuration file | en_US.UTF-8lc_monetary | configuration file | en_US.UTF-8lc_numeric | configuration file | en_US.UTF-8lc_time | configuration file | en_US.UTF-8log_line_prefix | configuration file | %t [%p-%l] %q%u@%dlog_timezone | configuration file | PRCmax_connections | configuration file | 100port | configuration file | 5432shared_buffers | configuration file | 16384ssl | configuration file | onssl_cert_file | configuration file | /etc/ssl/certs/ssl-cert-snakeoil.pemssl_key_file | configuration file | /etc/ssl/private/ssl-cert-snakeoil.keystats_temp_directory | configuration file | /var/run/postgresql/9.5-main.pg_stat_tmpTimeZone | configuration file | PRCunix_socket_directories | configuration file | /var/run/postgresqlmax_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 | 顾留芳 | 72 | 林业平 | 63 | 徐长卿 | 7
(3 行记录)test=##批量更新数据
test=# update student set studentage =6;
UPDATE 3
test=#
test=# select studentid,studentname,studentage from student;studentid | studentname | studentage
-----------+-------------+------------1 | 顾留芳 | 62 | 林业平 | 63 | 徐长卿 | 6
(3 行记录)test=#
转载于:https://my.oschina.net/u/1011130/blog/839736
postgresql 随笔相关推荐
- PostgreSQL随笔
数据库常用访问技术 ODBC:open datebase connectivity(开放数据库连接) JDBC:Java database connectivity(Java数据库连接) ADO.NE ...
- 《A Tour of PostgreSQL Internals》学习笔记——进程间通信
中秋节假期这么快就没了,这几天还一直下雨,索性在家看看书.这次看的是Tom Lane的<A Tour of PostgreSQL Internals>.这篇小随笔就算做学习笔记了.园子里面 ...
- 软件开发随笔系列二——关于架构和模型
软件开发随笔系列二--关于架构和模型 文章目录 软件开发随笔系列二--关于架构和模型 软件模型 功能模型 概念层 边界 参与方 分组分类 逻辑层 功能组织图 层次.模块化 接口 流程模型 概念层 业务 ...
- PostgreSQL索引介绍
梦中彩虹 博客园 首页 新随笔 联系 管理 随笔 - 131 文章 - 1 评论 - 14 PostgreSQL索引介绍 INDEX 索引是增强数据库性能的常用方法.索引使得数据库在查找和检索数据 ...
- Postgresql 日志收集
PG安装完成后默认不会记录日志,必须修改对应的(${PGDATA}/postgresql.conf)配置才可以,这里只介绍常用的日志配置. 1.logging_collector = on/off - ...
- pg数据库开启远程连接_如何运行远程客户端连接postgresql数据库
如何运行远程客户端连接 postgresql 数据库 前提条件是 2 个: 1 , pg_hba.conf 里面配置了运行远程客户机连接 pg_hba.conf 配置后需要重新加载 reload 生效 ...
- Postgresql:删除及查询字段中包含单引号的数据
Postgresql:删除及查询字段中包含单引号的数据 1. 假设pg表t_info的属性att,值为固定的:'test' 2. 假设值为不固定的,'abcde' 参考 1. 假设pg表t_info的 ...
- postgresql Insert插入的几个报错
postgresql Insert插入的几个报错 1. org.postgresql.util.PSQLException: 未设定参数值 2 的内容. 2. postgresql : column ...
- 【Postgresql】触发器某个字段更新时执行,行插入或更新执行
[Postgresql]触发器某个字段更新时执行,行插入或更新执行 1. postgresql触发器 2. 触发器的创建及示例 1) 字段更新时,触发 2) 行插入或更新时,触发 3. 触发器的删除 ...
最新文章
- 抽屉效果----mmdrawercontroller和viewdeck
- E431 笔记本电池问题 0190 Critical low-battery error 解决办法
- 项目实战-linux下安装activeMQ
- MongoDB【快速入门】
- PHP新浪乐居面试题,一个朋友去新浪乐居面试时的面试题(PHP)
- 算法练习day18——190409(Manacher)
- redis钟setnx命令
- P3385-[模板]负环【SPFA】
- Django框架—富文本编辑器
- 多个Email的JS检测正刚表达式.
- Duplicate property mapping of contactPhone found in
- [C++优先队列模板应用一] 哈夫曼树
- 阿里云nginx站点http升级https
- oracle设置禁用外键,oracle禁用表外键
- cad卸载_3分钟搞定CAD无法卸载问题,CAD卸载不再是烦恼
- Java无法加载主类
- CleanMyMac X免费的c盘清理工具
- 安装anaconda时报错“failed to create menus”尝试各种解决方法
- 集成测试和单元测试覆盖率合并
- 传统网吧风光不再 玩家上座率不足50%