【PostgreSQL灵活使用psql执行SQL的一些方式】

2024-02-01 17:28

本文主要是介绍【PostgreSQL灵活使用psql执行SQL的一些方式】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、psql执行SQL并使用选项灵活输出结果

可以不进入数据库,在命令行,使用psql 的-c选项跟上需要执行的SQL。来获取SQL的执行结果

postgres@ubuntu-linux-22-04-desktop:~$ psql -c "select 1,2" ?column? | ?column?
----------+----------1 |        2
(1 row)//同一个 -c的引号里是同一个事务,多个-c分别是不同的事物postgres@ubuntu-linux-22-04-desktop:~$ psql -c "select txid_current(); select txid_current();" -c "select txid_current();"txid_current
--------------1865
(1 row)txid_current
--------------1865
(1 row)txid_current
--------------1866
(1 row)

可以使用psql的选项对查询结果进行一些处理

//-A设置非对齐输出模式,加上-A后输出格式变得不对齐了,并且返回结果中没有空行
postgres@ubuntu-linux-22-04-desktop:~$ psql -c "select 1,2" -A
?column?|?column?
1|2
(1 row)// -t只显示数据,不显示表头和返回行数,但是有空行
postgres@ubuntu-linux-22-04-desktop:~$ psql -c "select 1,2" -t1 |        2//-t和-A两个同时加,则没有空行,非对齐输出
postgres@ubuntu-linux-22-04-desktop:~$ psql -c "select 1,2" -tA
1|2//可以使用-F更改分割符
postgres@ubuntu-linux-22-04-desktop:~$ psql -c "select 1,2" -tA -F ,
1,2//-q不显示输出信息。默认情况下psql执行命令是会返回多种信息,使用-q参数后将不显示这些信息
//-q选项通常和-c和-f一起使用,在维护操作中非常有用,当输出信息不重要时,这个特性非常重要

二、特殊字符转义问题

有时候如果想执行的SQL涉及到一些特殊字符,原本的-c可能执行涉及到转义的问题,这种情况,可以借助psql<<EOF < exec SQL> EOF这种来避免特殊字符转义问题。

postgres@ubuntu-linux-22-04-desktop:~$ psql -c "select * from  pg_stat_activity where query !~ 'COPY' and wait_event like '%BgWriterMain%'"
-bash: !~: event not foundpostgres@ubuntu-linux-22-04-desktop:~$ psql <<EOF
select * from  pg_stat_activity where query !~ 'COPY' and wait_event like '%BgWriterMain%'
EOFdatid | datname | pid  | leader_pid | usesysid | usename | application_name | client_addr | client_hostname | client_port |         backend_start
| xact_start | query_start | state_change | wait_event_type |  wait_event  | state | backend_xid | backend_xmin | query_id | query |   backend_type
-------+---------+------+------------+----------+---------+------------------+-------------+-----------------+-------------+-------------------------------+------------+-------------+--------------+-----------------+--------------+-------+-------------+--------------+----------+-------+-------------------|         | 2039 |            |          |         |                  |             |                 |             | 2024-02-01 13:41:00.395458+08 |            |             |              | Activity        | BgWriterMain |       |             |              |          |       | background writer
(1 row)

三、其他执行方式(psql结合管道符和echo)

postgres@ubuntu-linux-22-04-desktop:~$  echo '\encoding SQL-ASCII \\ SELECT relname, relnamespace FROM pg_class LIMIT 1;' | psqlrelname    | relnamespace
--------------+--------------pg_statistic |           11
(1 row)postgres@ubuntu-linux-22-04-desktop:~$ echo 'SELECT relname, relnamespace FROM pg_class LIMIT 1;'  'select txid_current();' 'select txid_current();'|psqlrelname    | relnamespace
--------------+--------------pg_statistic |           11
(1 row)txid_current
--------------1861
(1 row)txid_current
--------------1862
(1 row)//如果使用这种方式显式开启了事务,需要最后加上一个commit来提交事务,否则事务不会自动提交,执行之后自动回滚。postgres@ubuntu-linux-22-04-desktop:~$ echo 'begin;' 'select * from tab_test_1;' 'select txid_current();' 'insert into tab_test_1 values(1);' 'select txid_current();'|psql
BEGINid
----
(0 rows)txid_current
--------------1893
(1 row)INSERT 0 1txid_current
--------------1893
(1 row)postgres@ubuntu-linux-22-04-desktop:~$ psql -c "select * from tab_test_1"id
----
(0 rows)postgres@ubuntu-linux-22-04-desktop:~$ echo 'begin;' 'select * from tab_test_1;' 'select txid_current();' 'insert into tab_test_1 values(1);' 'select txid_current();' 'commit'|psql
BEGINid
----
(0 rows)txid_current
--------------1894
(1 row)INSERT 0 1txid_current
--------------1894
(1 row)COMMIT
postgres@ubuntu-linux-22-04-desktop:~$ psql -c "select * from tab_test_1"id
----1
(1 row)

四、交互式执行SQL或者脚本,逐步检查

执行SQL或者SQL脚本时候带上 --single-step或者-s 选项,可以交互式地运行它,同时逐行检查 SQL 文件的内容。这对于脚本调试和演示很有用。

回车表示执行SQL,x表示不执行SQL。

交互单步执行SQL,每一个-c 相当于一步

postgres@ubuntu-linux-22-04-desktop:~$ psql -c "select 1" -c "select 2" -s
***(Single step mode: verify command)*******************************************
select 1
***(press return to proceed or enter x and return to cancel)********************?column?
----------1
(1 row)***(Single step mode: verify command)*******************************************
select 2
***(press return to proceed or enter x and return to cancel)********************
x

交互单步执行SQL脚本。

postgres@ubuntu-linux-22-04-desktop:~$ cat check.sql
select 1;
select now();
select 2;
select now();postgres@ubuntu-linux-22-04-desktop:~$ psql -s -f check.sql
***(Single step mode: verify command)*******************************************
select 1;
***(press return to proceed or enter x and return to cancel)********************?column?
----------1
(1 row)***(Single step mode: verify command)*******************************************
select now();
***(press return to proceed or enter x and return to cancel)********************now
-------------------------------2024-02-01 14:39:40.523424+08
(1 row)***(Single step mode: verify command)*******************************************
select 2;
***(press return to proceed or enter x and return to cancel)********************
x
***(Single step mode: verify command)*******************************************
select now();
***(press return to proceed or enter x and return to cancel)********************
x

这篇关于【PostgreSQL灵活使用psql执行SQL的一些方式】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/667940

相关文章

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

MySQL进阶之路索引失效的11种情况详析

《MySQL进阶之路索引失效的11种情况详析》:本文主要介绍MySQL查询优化中的11种常见情况,包括索引的使用和优化策略,通过这些策略,开发者可以显著提升查询性能,需要的朋友可以参考下... 目录前言图示1. 使用不等式操作符(!=, <, >)2. 使用 OR 连接多个条件3. 对索引字段进行计算操作4

MySQL表锁、页面锁和行锁的作用及其优缺点对比分析

《MySQL表锁、页面锁和行锁的作用及其优缺点对比分析》MySQL中的表锁、页面锁和行锁各有特点,适用于不同的场景,表锁锁定整个表,适用于批量操作和MyISAM存储引擎,页面锁锁定数据页,适用于旧版本... 目录1. 表锁(Table Lock)2. 页面锁(Page Lock)3. 行锁(Row Lock

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线

在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题记录

《在SpringBoot中使用异步线程时的HttpServletRequest复用问题记录》文章讨论了在SpringBoot中使用异步线程时,由于HttpServletRequest复用导致... 目录一、问题描述:异步线程操作导致请求复用时 Cookie 解析失败1. 场景背景2. 问题根源二、问题详细分

从零教你安装pytorch并在pycharm中使用

《从零教你安装pytorch并在pycharm中使用》本文详细介绍了如何使用Anaconda包管理工具创建虚拟环境,并安装CUDA加速平台和PyTorch库,同时在PyCharm中配置和使用PyTor... 目录背景介绍安装Anaconda安装CUDA安装pytorch报错解决——fbgemm.dll连接p

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

使用Python创建一个能够筛选文件的PDF合并工具

《使用Python创建一个能够筛选文件的PDF合并工具》这篇文章主要为大家详细介绍了如何使用Python创建一个能够筛选文件的PDF合并工具,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录背景主要功能全部代码代码解析1. 初始化 wx.Frame 窗口2. 创建工具栏3. 创建布局和界面控件4

一文详解如何在Python中使用Requests库

《一文详解如何在Python中使用Requests库》:本文主要介绍如何在Python中使用Requests库的相关资料,Requests库是Python中常用的第三方库,用于简化HTTP请求的发... 目录前言1. 安装Requests库2. 发起GET请求3. 发送带有查询参数的GET请求4. 发起PO