Skip to content

Commit 547a0b5

Browse files
committed
post: postgres 03 - psql usage
1 parent 71e286e commit 547a0b5

File tree

26 files changed

+462
-91
lines changed

26 files changed

+462
-91
lines changed

content/posts/2025-11-15_postgresql-03-psql.md

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
+++
22
date = '2025-11-14T8:00:00+01:00'
3-
draft = true
3+
draft = false
44
title = 'Postgresql psql Tool Introduction'
55
tags = ['PostgreSQL']
66
+++
77

8+
### psql 介绍
9+
810
psql 是 PostgreSQL 中的一个交互式命令行工具,类似 Oracle 的 sqlplus,它允许用户交互式输入 SQL 语句或命令,然后发送给 PostgreSQL 服务器,再显示结果。
911
此外,psql 还有大量类似 shell 的特性来编写脚本,实现自动化操作。
1012

@@ -14,3 +16,96 @@ psql 是 PostgreSQL 中的一个交互式命令行工具,类似 Oracle 的 sql
1416
psql 也支持使用命令行参数来查询信息和执行 SQL,这种非交互模式与 linux 命令就没有太大区别了。
1517
例如使用 `psql -l` 查看数据库,当然也可以通过命令进入数据库后,输出 `\l` 命令查看有哪些数据库。
1618
默认会有一个叫 postgres 的数据库,这是默认安装后就有的一个数据库。
19+
还有两个模板数据库,template0 和 template1。
20+
21+
当用户创建创建数据库时,是从模板数据库 template1 克隆来的,因此通常可以订制 template1 中的内容,例如添加一些表和函数,这样后续创建的数据库也会有这些表和函数。
22+
template0 是一个最简化的数据库,如果指明从此数据库克隆,将创建出一个最简化的数据库。
23+
24+
使用 `\d` 查看有哪些表,使用 `\c` 连接到某数据库。连接命令格式如下:
25+
26+
```bash
27+
psql -h <hostname or ip> -p <port> [数据库名称] [用户名称]
28+
```
29+
30+
也可以通过环境变量指定
31+
32+
```bash
33+
export PGDATABASE=db_name
34+
export PGHOST=ip
35+
export PGPORT=port
36+
export PGUSER=postgres
37+
```
38+
39+
### psql 常用命令
40+
41+
- `\h` 命令查询 SQL 语法
42+
43+
```postgres
44+
\h CREATE USER
45+
```
46+
47+
- `\d [ pattern ]`: 该命令将显示每个匹配 pattern (表、视图、索引、序列) 的信息,包括对象中的所有列、各列的数据类型、表空间和所有特殊属性等。
48+
- 如果 `\d` 后面什么都不带,会显示当前数据库中的所有表
49+
- 如果 `\d` 后面根一个表名,会显示这个表的结构定义
50+
- `\d` 也可以显示索引信息
51+
- `\d` 后面的表明或索引名也可以使用通配符,如 `*``?`
52+
- `\d+` 可以显示更加详细的信息
53+
- `\dt` 只显示匹配的列表;`\di` 只显示索引;`\ds` 只显示序列;`\dv` 只显示视图;`\df` 只显示函数
54+
- `\timing` 让执行的 SQL 语句显示消耗的时间
55+
- `\dn` 列出所有的 schema
56+
- `db` 显示所有表空间
57+
- `\du``\dg` 列出数据库中所有角色或用户
58+
- `\dp``\z` 显示权限分配情况
59+
60+
- 指定客户端字符集
61+
当客户端的字符编码与服务器不一致时,可能出现乱码,可以使用 `\encoding` 命令指定客户端的字符编码。
62+
例如 `\encoding bgk;` 命令设置客户端的字符串编码为 `gbk``\encoding utf8;` 命令则设置编码为 `utf8`
63+
64+
- 格式化输出命令
65+
`\pset` 命令格式化输出
66+
67+
```psql
68+
\pset [option [value]]
69+
```
70+
71+
命令后的 option 和 value 参数可以设置多种不同的输出格式,下面介绍一些常见用法:
72+
- `\pset border 2` 输出和 MySQL 一样的外边框的内容,如果要修改回不带边框的内容,使用命令 `\pset border 0`,1 则是只有内边框
73+
- `\pset fieldsep` 设置分隔符(默认为 `|`
74+
- `\t` 删除头信息
75+
- `\o <filename>` 将 SQL 命令输出到文件,而不是终端
76+
- `\x` 将按行展示的数据改成按列展示,如果数据太长,就可以使用 `\x` 命令分为多行显示,类似 MySQL 的 `\G` 功能
77+
78+
- 执行存储在外部文件中的 SQL 命令
79+
- `\i <filename>` 用于执行存储在外部文件中的 SQL 命令
80+
- 也可以在 psql 命令行通过 -f 参数实现:`psql -f <filename>`
81+
82+
- 编辑命令
83+
`\e` 可以用于编辑文件,可以用于编辑系统中已存在的函数或视图定义。
84+
该命令会调用一个编辑器,通常是 vi / vim,不带任何参数时会打开一个临时文件。
85+
在里面输出 SQl 语句,退出后就会执行该命令,但在 psql 中是看不到的。
86+
`\e` 也可以指定一个文件名,但要求这个文件必须存在,否则会报错。
87+
88+
可以使用 `\ef` 命令编辑一个函数的定义,如果后面参数为空会出现一个编辑函数的模板。
89+
如果后面跟一个文件名,则函数定义的内容会出现在编辑器中,`:wq` 退出后,在输入 `;` 就会执行创建函数的 SQL 指令。
90+
91+
同样输入 `\ev` 且不带任何参数时,会出现一个创建视图的模板。编辑后退出,输入 `;` 执行视图 SQL 指令。
92+
93+
最后注意,退出后需要在 psql 中输入 `\reset` 来清除 psql 的命令缓冲区,防止误执行创建函数和视图的 SQL 语句。
94+
95+
- 输出信息命令
96+
`\echo` 命令用于输出一行信息,例如:
97+
98+
```
99+
\echo hello world
100+
```
101+
102+
此命令常用语 `.sql` 脚本文件中输出提示信息。
103+
104+
- 其他命令
105+
更多其他命令可以使用 `/?` 命令来显示
106+
107+
对于上方命令,如果需要获取对应的 SQL 命令,可以在启动时加上 `-E` 参数:
108+
109+
```SQl
110+
psql -E postgresql
111+
```

public/archives/index.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
<span class="max-w-[4rem] md:max-w-none truncate">Home</span></a></li><li class="flex items-center gap-1 md:gap-2 min-w-0"><span class="text-muted-foreground/50 flex-shrink-0"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/></svg>
5252
</span><span class="text-foreground flex items-center gap-0.5 md:gap-1 font-medium min-w-0 flex-shrink-0"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 8h14M5 8a2 2 0 110-4h14a2 2 0 110 4M5 8v10a2 2 0 002 2h10a2 2 0 002-2V8m-9 4h4"/></svg>
5353
<span class="max-w-[3rem] md:max-w-none truncate">Archives</span></span></li></ol></nav><header class=mb-8><div class="mb-4 flex items-center gap-3"><svg class="h-6 w-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 8h14M5 8a2 2 0 110-4h14a2 2 0 110 4M5 8v10a2 2 0 002 2h10a2 2 0 002-2V8m-9 4h4"/></svg><h1 class="text-foreground text-3xl font-bold">Archives</h1></div><p class="text-muted-foreground mb-6">Browse all articles in chronological order and discover what interests you.</p><div class="text-muted-foreground flex items-center gap-4 text-sm"><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/></svg>
54-
<span>76 posts total</span></div><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
55-
<span>Timeline view</span></div></div></header><div class=relative><div class="bg-border absolute top-0 bottom-0 left-4 w-0.5"></div><div class=mb-12><div class="relative mb-8 flex items-center"><div class="bg-primary absolute left-0 z-10 flex h-8 w-8 items-center justify-center rounded-full"><svg class="h-4 w-4 text-primary-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg></div><div class=ml-12><h2 class="text-foreground text-2xl font-bold">2025</h2><p class="text-muted-foreground text-sm">74
56-
posts</p></div></div><div class="relative mb-8"><div class="relative mb-4 flex items-center"><div class="bg-accent border-background absolute left-2 z-10 h-4 w-4 rounded-full border-2"></div><div class=ml-12><h3 class="text-foreground text-lg font-semibold">November 2025</h3><p class="text-muted-foreground text-xs">15
54+
<span>77 posts total</span></div><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
55+
<span>Timeline view</span></div></div></header><div class=relative><div class="bg-border absolute top-0 bottom-0 left-4 w-0.5"></div><div class=mb-12><div class="relative mb-8 flex items-center"><div class="bg-primary absolute left-0 z-10 flex h-8 w-8 items-center justify-center rounded-full"><svg class="h-4 w-4 text-primary-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg></div><div class=ml-12><h2 class="text-foreground text-2xl font-bold">2025</h2><p class="text-muted-foreground text-sm">75
56+
posts</p></div></div><div class="relative mb-8"><div class="relative mb-4 flex items-center"><div class="bg-accent border-background absolute left-2 z-10 h-4 w-4 rounded-full border-2"></div><div class=ml-12><h3 class="text-foreground text-lg font-semibold">November 2025</h3><p class="text-muted-foreground text-xs">16
5757
posts</p></div></div><div class="ml-12 space-y-3"><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/claude-code-%E5%88%86%E6%9E%90-09llm%E8%A7%86%E8%A7%92/ class=block>Claude Code 分析 09:LLM视角</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
5858
<time datetime=2025-11-14>11-14</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
5959
<span>13
@@ -84,6 +84,9 @@
8484
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/claude-code-%E5%88%86%E6%9E%90-00%E4%B8%BB%E6%96%87%E7%AB%A0/ class=block>Claude Code 分析 00:主文章</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
8585
<time datetime=2025-11-14>11-14</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
8686
<span>5
87+
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/postgresql-psql-tool-introduction/ class=block>Postgresql psql Tool Introduction</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
88+
<time datetime=2025-11-14>11-14</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
89+
<span>4
8790
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/complete-python-logging-guide/ class=block>Complete Python Logging Guide</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
8891
<time datetime=2025-11-11>11-11</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
8992
<span>7

0 commit comments

Comments
 (0)