PSQL基本使用(定製PSQL提示符)
環境描述:
作業系統: CentOS Linux release 7.6.1810 (Core)
postgresql版本 postgres (PostgreSQL) 12.4
host1 192.168.112.200
host2 192.168.112.201
首先翻看 postgresql(12.4) 官方文件 (Part VI.Reference => PostgreSQL Client Applications =>psql) 的 Prompting 部分描述
也可以參考 postgresql(12.4) 的原始碼 src/bin/psql/prompt.c 檔案中 get_prompt 的註釋部分
Prompting
The prompts psql issues can be customized to your preference. The three variables PROMPT1,PROMPT2, and PROMPT3 contain strings and special escape sequences that describe the appearance of the prompt. Prompt 1 is the normal prompt that is issued when psql requests a new command. Prompt 2 is issued when more input is expected during command entry, for example because the command was not terminated with a semicolon or a quote was not closed. Prompt 3 is issued when you are running an SQL COPY FROM STDIN command and you need to type in a row value on the terminal.The value of the selected prompt variable is printed literally, except where a percent sign (%) is encountered. Depending on the next character, certain other text is substituted instead. Defined substitutions are:
%M
The full host name (with domain name) of the database server, or [local] if the connection is over a Unix domain socket, or [local:/dir/name], if the Unix domain socket is not at the compiled in default location.
%m
The host name of the database server, truncated at the first dot, or [local] if the connection is over a Unix domain socket.
%>
The port number at which the database server is listening.
%n
The database session user name. (The expansion of this value might change during a database session as the result of the command SET SESSION AUTHORIZATION.)
%/
The name of the current database.
%~
Like %/, but the output is ~ (tilde) if the database is your default database.
%#
If the session user is a database superuser, then a #, otherwise a >. (The expansion of this value might change during a database session as the result of the command SET SESSION AUTHORIZATION.)
%p
The process ID of the backend currently connected to.
%R
In prompt 1 normally =, but @ if the session is in an inactive branch of a conditional block, or ^ if in single-line mode, or ! if the session is disconnected from the database (which can happen if \connect fails). In prompt 2 %R is replaced by a character that depends on why psql expects more input: - if the command simply wasn't terminated yet, but * if there is an unfinished / * ... */ comment, a single quote if there is an unfinished quoted string, a double quote if there is an unfinished quoted identifier, a dollar sign if there is an unfinished dollar-quoted string,or ( if there is an unmatched left parenthesis. In prompt 3 %R doesn't produce anything.
%x
Transaction status: an empty string when not in a transaction block, or * when in a transaction block, or ! when in a failed transaction block, or ? when the transaction state is indeterminate (for example, because there is no connection).
%l
The line number inside the current statement, starting from 1.%digits The character with the indicated octal code is substituted.
%:name:
The value of the psql variable name. See the section Variables for details.
%`command`
The output of command, similar to ordinary “back-tick” substitution.
%[ ... %]
Prompts can contain terminal control characters which, for example, change the color,background, or style of the prompt text, or change the title of the terminal window. In order for the line editing features of Readline to work properly, these non-printing control characters must be designated as invisible by surrounding them with %[ and %]. Multiple pairs of these can occur
within the prompt. For example:
testdb=> \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# '
results in a boldfaced (1;) yellow-on-black (33;40) prompt on VT100-compatible, colorcapable terminals.
To insert a percent sign into your prompt, write %%. The default prompts are '%/%R%# ' for prompts 1 and 2, and '>> ' for prompt 3.
首先PROMPT1\PROMPT2\PROMPT3 的區別:
Prompt 1 is the normal prompt that is issued when psql requests a new command
Prompt 2 is issued when more input is expected during command entry
Prompt 3 is issued when you are running an SQL COPY FROM STDIN command and you need to type in a row value on the terminal.
PROMPT 的預設值:
The default prompts are '%/%R%# ' for prompts 1 and 2, and '>> ' for prompt 3.
我們通過實驗進行一下測試.
Prompt 1:
[postgres@pg1 ~]$ psql -h 192.168.112.201
psql (12.4)
Type "help" for help.
postgres=# \set PROMPT1 '%M:%[%033[1;31m%]%>%[%033[0m%] %n@%/%R%#%x ' <= 配置前
192.168.112.201:5432 postgres@postgres=# <= 配置後
Prompt 2:
配置前:
postgres=# select *
postgres-# from
postgres-# test
postgres-# ;
配置後:
postgres=# \set PROMPT2 '%M %n@%/%R %# '
postgres=# select *
192.168.112.201 postgres@postgres- # from
192.168.112.201 postgres@postgres- # test
192.168.112.201 postgres@postgres- # ;
Prompt 3:
配置前:
postgres=# copy test from stdin;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>> 1
>> 2
>> 3
>> \.
COPY 3
配置後:
postgres=# \set PROMPT3 '%M %n@%/%R %# '
postgres=# copy test from stdin;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
192.168.112.200 postgres@postgres # 1
192.168.112.200 postgres@postgres # 2
192.168.112.200 postgres@postgres # 3
192.168.112.200 postgres@postgres # \.
COPY 3
帶日期等個性化配置:
[postgres@pg1 ~]$ psql -h 192.168.112.201
psql (12.4)
Type "help" for help.
postgres=# \set PROMPT1 '%`date +%H:%M:%S` (%n@%M:%>)%/%R%#%x '
21:13:55 (postgres@192.168.112.201:5432)postgres=#
配置固化到配置檔案:
1.在/home/postgres 下建立 .psqlrc 檔案.
\set PROMPT1 '%`date +%H:%M:%S` (%n@%M:%>)%/%R%#%x '
\set PROMPT2 '%M %n@%/%R %# '
\set PROMPT3 '%M %n@%/%R %# '
2.根據postgresql原始碼檔案 src/bin/psql/psqlrc.sample 檔案內容
--
-- system-wide psql configuration file
--
-- This file is read before the .psqlrc file in the user's home directory.
--
-- Copy this to your installation's sysconf directory and rename it psqlrc.
-- The sysconf directory can be identified via "pg_config --sysconfdir".
[postgres@pg1 ~]$ pg_config --sysconfdir
/pgsoft/etc/postgresql
可見 /pgsoft/etc/postgresql 目錄下的 psqlrc 檔案是優先於 .psqlrc 進行讀取的.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7569309/viewspace-2751279/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PostgreSQL 定製psql提示符SQL
- PSQL基本使用(定製維護指令碼)SQL指令碼
- 【PG_PSQL】PSQL使用測試SQL
- psql 中使用 gexecSQL
- psql: symbol lookup error: ./psql: undefined symbol: PQsetErrorContextVisibilitySQLSymbolErrorUndefinedContext
- PostgreSQL PSQL tipsSQL
- How to Install psql on MacSQLMac
- PostgreSQL:psql 介紹SQL
- psql: error: could not connect to server: No such file or directorySQLErrorServer
- PostgreSQL DBA(148) - pgAdmin(Show script for psql command)SQL
- ror+psql+puma+nginx部署專案SQLNginx
- PostgreSQL DBA(105) - pgAdmin(Don't do this:psql&inheritance)SQL
- postgresql 11版本psql命令幫助大全SQL
- PostgreSQL DBA(137) - PG 13(Allow invisible PROMPT2 in psql)SQL
- PostgreSQL提示:‘psql: FATAL: ”Peer authentication failed for user ”postgres“’錯誤SQLAI
- psql: 錯誤: 致命錯誤: 對使用者“postgres“的對等認證失敗SQL
- psql: 致命錯誤: 對使用者"user1"的對等認證失敗SQL
- PostgreSQL邏輯備份恢復--pg_dump匯出及psql匯入案例SQL
- csh Prompt全描述--定製自己的linux命令列提示符Linux命令列
- Linux的自定製shell提示符Linux
- 母盤製作-母盤基本設定
- 使用 Dockerfile 定製映象Docker
- 使用 Dockerfile 定製映象【轉】Docker
- logrus的使用及格式定製
- Spring之定時任務基本使用篇Spring
- 函式基本定義和使用‘未完待續函式
- 使用者定製再度興起,定製產品小程式將迎來春天!
- AndroidStudio基本設定,以及一些使用小技巧Android
- VS Code使用之基本設定與配置詳解
- 04-spring-boot-resttemplate netty定製使用SpringbootRESTNetty
- Linux命令列提示符是什麼?如何使用?Linux命令列
- ADS使用:layout後聯合模擬的基本設定
- log4j的基本使用和引數設定
- .NET中使用DebuggerDisplay輕鬆定製除錯除錯
- Python基本圖形繪製--模組1:turtle庫的使用Python
- 定製Tinycore
- 多使用者商城系統定製開發
- 使用 PubSubHubbub 製作 RSS 定時器 —— Laravel RSS (三)定時器Laravel