如何在Oracle、MySQL、PostgreSQL中改变SQL提示格式

How to Change in SQL Prompt format in Oracle , MySQL, PostgreSQL

像UNIX的PS1环境变量可以改变shell操作提示符, 在日常工作环境中可以提升一些效率可以防止一些误操作, 很多年前在看tom关于在练习oracle操作前的一些环境配置像login.sql, 比起"SQL >"还可以显示当前的用户或数据库名很是欣喜, 下面记录Oracle , MySQL, PostgreSQL三个主流数据库改变命令行提示符的方法。

Oracle

sqlplus中执行set sqlprompt或增加到logoin.sql/global.sql脚本中自动触发

sql 复制代码
col ver  new_value ver
col host new_value host
col inst new_value inst
set termout off
select substr(version,1,instr(version,'.',1,4)) ver,
       upper(instance_name) inst,
       substr(host_name,1,instr(host_name,'.',1,1) - 1) host
 from v$instance;
set termout on
--set sqlprompt "&&ver&&_USER@&&_CONNECT_IDENTIFIER SQL> "
set sqlprompt "&&ver&&_USER@&&host/&&inst SQL> "
set linesize 500 pagesize 2000 timi on
alter session set max_dump_file_size = unlimited;

-- or --

col "_myprompt" new_value myprompt
set termout off
select
    chr(27)||'[31m'||
    substr('&_USER@&_CONNECT_IDENTIFIER'||rtrim(' &_PRIVILEGE')||'/'||
        trunc(&_O_RELEASE/100000000) || '.' ||
        mod(trunc(&_O_RELEASE/1000000),100) || '.' ||
        mod(trunc(&_O_RELEASE/10000),100) || '.' ||
        mod(trunc(&_O_RELEASE/100),100),
        1, 36)
    ||chr(27)||'[m'||chr(10)||'SQL> ' "_myprompt"
from dual;
set termout on
set sqlp "&myprompt"


SQL> @login

Session altered.

Elapsed: 00:00:00.00
19.0.0.0.SYS@/ANBOB19C SQL>

MySQL

配置MYSQL_PS1 或 配置在/etc/my.cnf or .my.cnf 文件中

The MYSQL_PS1 in this example displays the following three information in the prompt:

shell 复制代码
#Generic variables:
\S displays semicolon
\' displays single quote
\" displays double quote
\v displays server version
\p displays port
\ displays backslash
\n displays newline
\t displays tab
\ displays space (there is a space after \ )
\d displays default database
\h displays default host
_ displays space (there is a underscore after \ )
\c displays a mysql statement counter. keeps increasing as you type commands.
\u displays username
\U displays username@hostname accountname

#Date related variables:
\D displays full current date (as shown in the above example)
\w displays 3 letter day of the week (e.g. Mon)
\y displays the two digit year
\Y displays the four digit year
\o displays month in number
\O displays 3 letter month (e.g. Jan)
\R displays current time in 24 HR format
\r displays current time in 12 hour format
\m displays the minutes
\s displays the seconds
\P displays AM or PM




$ export MYSQL_PS1="\u@\h [\d]> "
$ mysql -u root -pyour-password -D xxxCRM
root@dev-db [XXcrm]>
mysql> prompt (\u@\h) [\d] \R:\m:\s>\_

postgresql

在psql中配置set PROMPT1或配置到.psqlrc文件中

shell 复制代码
vi ~/.psqlrc
\set PROMPT1 '%M:%> %n@%/%R%#%x '


%M refers to the database server's hostname -- is "[local]" if the connection is over a Unix domain socket
%> refers to the listening port
%n refers to the session username
%/ refers the current database
%R refers to whether you're in single-line mode (^) or disconnected (!) but is normally =
%# refers to whether you're a superuser (#) or a regular user (>)
%x refers to the transaction status -- usually blank unless in a transaction block (*)


#Colors
#The prompt color can be edited with the psqlrc. To make the port number red add the following.
\set PROMPT1 '%M:%[%033[1;31m%]%>%[%033[0m%] %n@%/%R%#%x '

There are various colors you can use -- change the value 31 to:

  • 32 for green
  • 33 for yellow
  • 34 for blue
  • 35 for magenta
  • 36 for cyan
  • 37 for white

像UNIX PS1环境的配置参考Bash Shell PS1: 10 Examples to Make Your Linux Prompt like Angelina Jolie

openGauss

shell 复制代码
# openGauss提示符
cd
cat >.gsqlrc <<EOF
\set PROMPT1 '%n@%~%R%#'
\pset border 2
EOF
相关推荐
小苹果13571 小时前
阿里云mysql数据丢失,如何通过服务器备份在其他服务器上恢复数据,并获取mysql丢失数据,完成mysql数据恢复
服务器·mysql·阿里云
张璐月2 小时前
mysql join语句、全表扫描 执行优化与访问冷数据对内存命中率的影响
数据库·mysql
ruan1145142 小时前
MySQL4种隔离级别
java·开发语言·mysql
慕木兮人可9 小时前
Docker部署MySQL镜像
spring boot·后端·mysql·docker·ecs服务器
{⌐■_■}10 小时前
【Kafka】登录日志处理的三次阶梯式优化实践:从同步写入到Kafka多分区批处理
数据库·分布式·mysql·kafka·go
kfepiza12 小时前
Debian10安装Mysql5.7.44 笔记250707
笔记·mysql·debian
armcsdn13 小时前
基于Docker Compose部署Traccar容器与主机MySQL的完整指南
mysql·docker·容器
睿思达DBA_WGX13 小时前
由 DB_FILES 参数导致的 dg 服务器无法同步问题
运维·数据库·oracle
袋鼠云数栈14 小时前
使用自然语言体验对话式MySQL数据库运维
大数据·运维·数据库·后端·mysql·ai·数据治理·数栈·data+ai
渣渣盟14 小时前
掌握MySQL函数:高效数据处理指南
sql·mysql·adb·dba