網(wǎng)站首頁 編程語言 正文
PostgreSQL實(shí)時(shí)查看數(shù)據(jù)庫實(shí)例正在執(zhí)行的SQL語句實(shí)例詳解_PostgreSQL
作者:技術(shù)很渣 ? 更新時(shí)間: 2023-03-03 編程語言一、查詢當(dāng)前正在執(zhí)行所有SQL語句
SELECT
pid,
datname,
usename,
client_addr,
application_name,
STATE,
backend_start,
xact_start,
xact_stay,
query_start,
query_stay,
REPLACE ( query, chr( 10 ), ' ' ) AS query
FROM
(
SELECT
pgsa.pid AS pid,
pgsa.datname AS datname,
pgsa.usename AS usename,
pgsa.client_addr client_addr,
pgsa.application_name AS application_name,
pgsa.STATE AS STATE,
pgsa.backend_start AS backend_start,
pgsa.xact_start AS xact_start,
EXTRACT ( epoch FROM ( now( ) - pgsa.xact_start ) ) AS xact_stay,
pgsa.query_start AS query_start,
EXTRACT ( epoch FROM ( now( ) - pgsa.query_start ) ) AS query_stay,
pgsa.query AS query
FROM
pg_stat_activity AS pgsa
WHERE
pgsa.STATE != 'idle'
AND pgsa.STATE != 'idle in transaction'
AND pgsa.STATE != 'idle in transaction (aborted)'
) idleconnections
ORDER BY
query_stay DESC
二、判斷是否存在慢查詢語句
字段 | 解釋 |
---|---|
PID | 數(shù)據(jù)庫查詢進(jìn)程ID |
query_stay | 查詢時(shí)長(zhǎng)秒 |
query | 查詢SQL語句 |
三、按查詢進(jìn)程,殺掉慢查詢釋放資源
SELECT pg_terminate_backend(PID);
SELECT pg_terminate_backend(6289);
四、擴(kuò)展query的顯示長(zhǎng)度能展示出全部語句
vi /var/lib/pgsql/12/data/postgresql.conf
修改track_activity_query_size的值重啟數(shù)據(jù)庫服務(wù)
五、殺掉某個(gè)數(shù)據(jù)庫的所有數(shù)據(jù)庫連接
SELECT pg_terminate_backend(pg_stat_activity.pid)FROM pg_stat_activity WHERE datname='數(shù)據(jù)庫名稱' AND pid<>pg_backend_pid();
六、導(dǎo)出數(shù)據(jù)庫某個(gè)表
pg_dump -t 某個(gè)表名 "host=192.168.16.31 port=18921 user=postgres password=數(shù)據(jù)庫密碼 dbname=數(shù)據(jù)庫" -f /app/zjfbeifen/1.sql
七、shell腳本自動(dòng)導(dǎo)入按表
touch insert_sql.sh
#!/bin/bash
. /etc/profile
. ~/.bash_profile
export PATH=/usr/pgsql-12/bin:/usr/bin;
export PGPASSWORD=數(shù)據(jù)庫密碼;psql -U postgres -d 數(shù)據(jù)庫名稱 < /mnt/nas/bak-06/1.sql;
指定postgres用戶執(zhí)行導(dǎo)入某個(gè)表的語句,免密執(zhí)行shell腳本
touch nasen_insert.sql
#!/bin/sh
source /etc/profile
PATH=/usr/local/bin:$PATH
echo 'task_start'
runuser - postgres -g postgres -c "sh /data/test/insert_sql.sh"
echo 'task_finish'
定時(shí)器配置
00 10 * * * /usr/bin/sh /data/test/insert_nasen.sh >>/data/test/insert_nasen.log
八、shell腳本執(zhí)行postgre的sql語句
touch drop01.sql
#刪除某個(gè)表SQL語句
DROP TABLE IF EXISTS public.test;
touch drop.sh
#!/bin/bash
. /etc/profile
. ~/.bash_profile
export PATH=/usr/pgsql-12/bin:/usr/bin;
#su postgres
export PGPASSWORD=123456;psql -U postgres -d 數(shù)據(jù)庫名稱 < /data/test/drop01.sql;
touch nasen_drop.sh
#!/bin/bash
source /etc/profile
PATH=/usr/local/bin:$PATH
echo 'task_start'
runuser - postgres -g postgres -c "sh /data/test/drop.sh"
echo 'task_finish'
定時(shí)器調(diào)用
00 9 * * * /usr/bin/sh /data/test/drop_nasen.sh >>/data/test/drop_nasen.log
九、導(dǎo)出導(dǎo)入整個(gè)數(shù)據(jù)庫
備份導(dǎo)出整個(gè)數(shù)據(jù)庫
pg_dump -h localhost -U postgres -d riskDataCheck -Fc -f /soft/backup/20220321/test-0321.dump
導(dǎo)入某個(gè)數(shù)據(jù)庫
pg_restore -h localhost -U postgres -d risk_agcloud_430000_1 /soft/backup/test-0321.dump
十、切換數(shù)據(jù)庫存儲(chǔ)路徑遷移data目錄
步驟1:創(chuàng)建新data目錄
sudo mkdir /home/data
sudo chown -R postgres:postgres data
sudo chmod 700 data
步驟2:關(guān)閉數(shù)據(jù)庫服務(wù)
systemctl stop postgresql-12
步驟3:執(zhí)行復(fù)制
cp -rf /var/lib/pgsql/12/data/* /home/data #這個(gè)路徑是默認(rèn)的在線安裝postgresql12.11默認(rèn)路徑
sudo chown -R postgres:postgres data #加這句是因?yàn)閺?fù)制過來有的時(shí)候歸屬是ROOT用戶,應(yīng)該是postgres用戶才對(duì)!
步驟4:修改服務(wù)配置文件
cd /usr/lib/systemd/system
vi postgresql-12.service #默認(rèn)的在線安裝postgresql12.11服務(wù)名稱
Environment=PGDATA=/home/data
步驟5:重新加載服務(wù)配置
systemctl daemon-reload
步驟6:重啟數(shù)據(jù)庫服務(wù)
systemctl start postgresql-12
步驟7:查看變更狀態(tài)
systemctl status postgresql-12
十一、安裝PostGIS插件
步驟1:安裝postgis的依賴包
rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
步驟2:安裝postgis
yum install postgis31_12.x86_64 #這個(gè)對(duì)版本有一些對(duì)應(yīng)關(guān)系注意,這個(gè)命令對(duì)應(yīng)的是12.X
步驟3:安裝完畢后切換為postgres用戶,開啟擴(kuò)展初始化操作
// 開啟插件
# su postgres
# psql
// 開啟pgsql的插件
postgres=# create extension postgis;
postgres=# create extension postgis_topology;
postgres=# create extension fuzzystrmatch;
postgres=# create extension address_standardizer;
postgres=# create extension address_standardizer_data_us;
postgres=# create extension postgis_tiger_geocoder;
//查看版本,驗(yàn)證安裝是否成功
postgres=# SELECT PostGIS_full_version();
步驟4:安裝pgRouting
yum install pgrouting_12 #針對(duì)12.X版本
步驟5:檢查插件是否完成
十二、安裝uuid-ossp插件
postgres=# create extension “uuid-ossp”;
ERROR: could not open extension control file “/opt/pgsql12.2/share/extension/uuid-ossp.control”: No such file or directory
步驟1:安裝uuid依賴包
[root@Location-01 ~]# yum -y install e2fsprogs-devel uuid uuid-devel libuuid-devel
## 步驟2:執(zhí)行編譯配置
[root@Location-01 ~]# cd /usr/local/src/postgresql-12.2/
[root@Location-01 postgresql-12.2]# pwd
/usr/local/src/postgresql-12.2
[root@Location-01 postgresql-12.2]# ./configure --prefix=/opt/pgsql12.2 --with-uuid=ossp
步驟3:編譯安裝uuid-ossp
[root@Location-01 ~]# cd /usr/local/src/postgresql-12.2/contrib/uuid-ossp/
[root@Location-01 uuid-ossp]# pwd
/usr/local/src/postgresql-12.2/contrib/uuid-ossp
[root@Location-01 uuid-ossp]# make && make install
步驟4:檢測(cè)是否成功
postgres=# create extension "uuid-ossp";
CREATE EXTENSION
postgres=# select * from pg_available_extensions;
name | default_version | installed_version | comment
------------+-----------------+-------------------+-------------------------------------------------
plpgsql | 1.0 | 1.0 | PL/pgSQL procedural language
plperl | 1.0 | | PL/Perl procedural language
plperlu | 1.0 | | PL/PerlU untrusted procedural language
plpython2u | 1.0 | | PL/Python2U untrusted procedural language
plpythonu | 1.0 | | PL/PythonU untrusted procedural language
uuid-ossp | 1.1 | | generate universally unique identifiers (UUIDs)
(6 rows)
總結(jié)
原文鏈接:https://blog.csdn.net/nasen512/article/details/125769140
相關(guān)推薦
- 2022-12-23 Kubernetes存儲(chǔ)系統(tǒng)數(shù)據(jù)持久化管理詳解_云其它
- 2022-04-10 關(guān)于C#中GUI編程的標(biāo)準(zhǔn)事件問題_C#教程
- 2022-09-06 C語言單鏈表遍歷與求和示例解讀_C 語言
- 2022-12-22 Python類繼承及super()函數(shù)使用說明_python
- 2022-09-15 C#獲取文件名和文件路徑的兩種實(shí)現(xiàn)方式_C#教程
- 2022-10-01 Golang?中的?unsafe.Pointer?和?uintptr詳解_Golang
- 2022-03-26 Postman傳遞對(duì)象參數(shù)(包含有集合對(duì)象)_相關(guān)技巧
- 2022-07-12 Git cherry-pick實(shí)現(xiàn)只復(fù)制某個(gè)commit
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支