惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Securelist
C
Cybersecurity and Infrastructure Security Agency CISA
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
Hacker News: Ask HN
Hacker News: Ask HN
L
Lohrmann on Cybersecurity
PCI Perspectives
PCI Perspectives
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog
月光博客
月光博客
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
The GitHub Blog
The GitHub Blog
Webroot Blog
Webroot Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
TaoSecurity Blog
TaoSecurity Blog
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
F
Full Disclosure
U
Unit 42
Jina AI
Jina AI
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
The Hacker News
The Hacker News
The Last Watchdog
The Last Watchdog
T
Troy Hunt's Blog
腾讯CDC
T
Threatpost
H
Hacker News: Front Page
P
Palo Alto Networks Blog
博客园 - 聂微东
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
Help Net Security
Help Net Security
L
LINUX DO - 热门话题
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Spread Privacy
Spread Privacy

暗无天日

读:AI Agent 安全日志——从可见性与隐私的两难说起 - 暗无天日 AI写作的语言指纹——如何让文字不那么像机器 - 暗无天日 读:50 条 Claude Code 技巧——一个工程经理的六个月使用心得 读:AI 辅助开发为什么让 E2E 测试更有价值 - 暗无天日 读:在Emacs中使用Claude Code(Spacemacs适配版) - 暗无天日 Claude Code 背后的工程哲学——读 Agent Harness Engineering 读:Agent Harness Engineering——AI 智能体不只是模型,还有套件 - 暗无天日 browser-harness:让 AI 直接接管你的浏览器 - 暗无天日 读:Security-First CI/CD —— DevSecOps 自动化实践指南 TIL: 数字小键盘的小数点陷阱与行内算术求值 - 暗无天日 读:Immutability 不是万能药,它是一种权衡 - 暗无天日 Conducty:给 Claude Code 加上项目记忆和并行执行能力 - 暗无天日 读 — GitHub Trending 里的 Claude Code 技能包 读 — Prompt Caching 省钱指南 TIL: Emacs 中那些跟鼠标配合的冷门快捷键 - 暗无天日 读:Anvil——把 Emacs 变成 AI 的工具服务器 读:Emacs 代码折叠终极指南 - 暗无天日 读:Clojure 搭车客指南 - 暗无天日 git推送失败后恢复仓库损坏的完整记录 - 暗无天日 多智能体系统的两个有效模式——以及对 Claude Code 用户的启示 - 暗无天日 用 Org Babel 写 Literate 博文:扩展执行 + 定制导出 proced:Emacs 内置的进程查看器 - 暗无天日 从 proced 定制中学到的 Elisp 模式 读:让 Emacs proced 在 macOS 上显示 CPU 和内存 异步编程的函数着色税 - 暗无天日 链式调用的代价:JavaScript 和 Clojure 的共同教训 - 暗无天日 hyperfine:命令行基准测试工具 - 暗无天日 管道中的变量去哪了?——子 shell 作用域陷阱 - 暗无天日 开源包装器的信任陷阱:四个危险信号 - 暗无天日 程序员愿意为 AI 写文档,却不愿为同事写 - 暗无天日 mktemp: Shell 脚本中临时文件的安全陷阱与最佳实践 - 暗无天日 WSL9x —— 在 Windows 9x 里跑 Linux 内核 6.19 用 ox.el 做你想做的事 —— org-export 高级编程指南 读:Hot-wiring the Lisp Machine —— 用纯 Elisp 构建零依赖的 Org 静态站点生成器 Elisp 性能优化的六个实战教训 - 暗无天日 fcitx5 下 Emacs 无法切换输入法的排查 - 暗无天日 ERT 测试交互命令的三种方式 - 暗无天日 SEM Assistant: 当 Elisp 守护进程遇上 LLM 用 dmsg 给 Elisp 加上结构化调试日志 用 org-habit 追踪非每日习惯 - 暗无天日 Clojure X-Men:当编程语言特性变成超能力 - 暗无天日 TIL: 用 diff-hl 在 fringe 中显示 git 变更 读:llm-test —— 用 LLM agent 驱动 Emacs 测试 TIL: AI 时代的橡皮鸭调试 - 暗无天日 fcitx 启动后键盘输入卡顿的排查 - 暗无天日 TIL: 早期网页的图片热区导航 - 暗无天日 读 Seeing the Whole System 用 Emacs 自动生成每周链接推荐 - 暗无天日 读:ASCII control characters in my terminal 读 What to learn - 暗无天日 Lisp 的括号之痛——一个愚人节玩笑揭开的老伤疤 - 暗无天日 一本书该"线性读"还是"并行读" - 暗无天日 读 How to Monetize a Blog:一篇伪装成变现指南的讽刺文 Python Mock 第三方依赖的四种策略 - 暗无天日 Emacs Lisp 热重载实用指南 - 暗无天日 Prot 的 Emacs 配置哲学 - 暗无天日 TIL: 从直播对谈中学到的三个 Emacs 技巧 - 暗无天日 TIL: 自动使用项目虚拟环境的 Python - 暗无天日 TIL: 让 Help buffer 自动获得焦点 一条命令让本地开发用上 HTTPS —— slim 工具介绍 用 fsck 检查和修复 Linux 文件系统 排查Linux进程"卡死"实战:从strace到gdb全流程 - 暗无天日 PostgreSQL 索引:从基础到你可能不知道的高级用法 - 暗无天日 用 .pdbrc 自定义 Python 调试器 ANSI 转义码的标准化现状 - 暗无天日 终端程序的潜规则 - 暗无天日 PARA Org-mode 测试配置 - 暗无天日 AI越强越辣鸡?控制论说这是必然的 - 暗无天日 AI 越强越需要你盯着——反馈循环实操指南 - 暗无天日 你的AI代理正在偷你的密钥——四种你没想到的泄露通道 - 暗无天日 LLM 在 DevOps 中的三种角色 - 暗无天日 写作风格的反建议 - 暗无天日 反驳本质复杂性——Dan Luu 论为什么《没有银弹》错了 - 暗无天日 文件充满了危险——Dan Luu 谈文件系统的可靠性陷阱 - 暗无天日 AI 时代的 PARA 方法:用 Org-mode 和 AI 打造个人知识管理系统 Linux 数据去重学习笔记 - 暗无天日 创建跨平台 ZIP 文件的隐藏陷阱:Extra Field - 暗无天日 X11 Forwarding 排障指南 - 暗无天日 IP欺骗端口扫描:当别人冒充你去扫描别人 - 暗无天日 Linux 输入栈全景解析:从硬件按键到屏幕响应 - 暗无天日 Unix 系统中那些被埋没的配置开关——以 FontConfig 为例 - 暗无天日 在Linux上限制儿童使用电脑 - 暗无天日 GIF不仅仅是一种图片格式——用GIF流做些奇怪的事 - 暗无天日 Leiningen 学习笔记:Clojure 项目构建与管理从入门到实战配置 - 暗无天日 Google SRE Book 读书笔记 - 暗无天日 yes 管道 head 发生了什么 - 暗无天日 为什么 nohup 在 crontab 中不起作用 Bash中的Indirection与Nameref - 暗无天日 Linux PAM 简介 - 暗无天日 从Linux ISO文件启动计算机 - 暗无天日 用 Bash 打造一个Screen Locker 用GitHub Actions自动构建EGO博客 - 暗无天日 blocking I/O 的作用 - 暗无天日 mobileog 手机端同步提示Error:2 No such file 的解决方法 回收 WSL2 VHDX 文件占用空间 使用 org-mode columnview 生成任务列表 - 暗无天日 Emacs 作为 MPD 客户端 - 暗无天日 移动文件路径却不破坏org file link的方法 - 暗无天日 如何合理的导出help link 成HTML - 暗无天日 笑话理解之Biology - 暗无天日
读:Emacs 连接数据库时密码放哪里 - 暗无天日
2026-05-05 · via 暗无天日

引子

用 Emacs 的 sql-mode 连数据库写查询,密码放哪里是个问题。写在配置里是明文暴露,每次手动输入又打断工作流。Magnus Therning 在 博客 上整理了两种做法,Stefano Rodighiero 也写了 自己的方案。两种思路不同,但最后都能在 Org Babel 里通过 :dbconnection 参数引用连接,Org 文件里不写密码。

方案一:pgpass 文件 + 解析函数

Stefano Rodighiero 的做法:把连接信息放在 ~/.pgpass 文件里(PostgreSQL 客户端库 libpq 默认读取的密码文件),用解析函数把它转成 sql-connection-alist (sql-mode 存储连接配置的变量)。

文件格式

Stefano 在标准 pgpass 格式( hostname:port:database:username:password )前面加了一行注释作为连接名:

# mydb
db.example.com:5432:mydb:myuser:mypassword
# analytics
analytics.example.com:5432:analytics:analyst:secretpass

标准 pgpass 本身支持 # 开头的注释行,所以这个文件同时兼容 PostgreSQL 客户端工具和 Stefano 的解析函数。

解析函数

(defun get-connection-alist (filename)
  "从 FILENAME (pgpass 格式) 解析出 sql-connection-alist."
  (with-current-buffer (find-file-noselect filename)
    (let ((lines (split-string (buffer-string) "\n" t)))
      (when lines
        (cl-loop for (k v) in (seq-partition lines 2)
          collect (cl-destructuring-bind (host port db user password)
                      (split-string v ":" nil)
                    `(,(replace-regexp-in-string "^#\s+" "" k)
                      (sql-product 'postgres)
                      (sql-port ,(string-to-number port))
                      (sql-server ,host)
                      (sql-user ,user)
                      (sql-database ,db)
                      (sql-password ,password))))))))

函数做的事:打开文件,按行分割,每两行为一组(注释行是名称,数据行是连接信息),解析数据行的冒号分隔字段,组装成 sql-connection-alist 需要的格式。

设置和自动重载

(setq sql-connection-alist (get-connection-alist "~/.pgpass"))

Stefano 用 Emacs daemon 模式(Emacs 后台常驻,客户端连上去用),Emacs 一跑可能就是几周。期间 pgpass 文件可能改了(比如换了密码、加了新库),他用 file-notify (Emacs 内置的文件变更监控)自动重载:

(file-notify-add-watch
 "~/.pgpass" '(change)
 (lambda (evt)
   (setq sql-connection-alist
         (get-connection-alist "~/.pgpass"))))

pgpass 文件一改, sql-connection-alist 自动更新,不用重启 Emacs。

特点

  • 连接信息和密码都在一个文件里,格式简单
  • 密码在内存中是明文(存在 sql-connection-alist 里)
  • pgpass 文件本身也是明文,需要注意文件权限(建议 chmod 600
  • 主要适合 PostgreSQL 系数据库

方案二:authinfo.gpg + auth-source

Harald J 的做法:密码存在 ~/.authinfo.gpg 里(GPG 加密的 authinfo 文件),sql-mode 需要密码时通过 auth-source 查找。auth-source 是 Emacs 内置的统一密码存储接口,支持 netrc 文件、GPG 加密文件、系统密钥环等后端。连接定义里不出现密码。

authinfo 文件格式

machine db.example.com/mydb login myuser password mypassword port 5432
machine analytics.example.com/analytics login analyst password secretpass port 5432

machine 字段用 host/dbname 格式(主机名和数据库名用斜杠拼接),用来区分同一主机上的不同数据库。这个文件用 GPG 加密后存为 ~/.authinfo.gpg

自定义密码查找函数

(defun my/sql-auth-source-search-wallet (wallet product user server database port)
  "从 auth-source WALLET 中查找 USER 的密码。
DATABASE 和 SERVER 用斜杠拼接作为 host 查找键。"
  (when-let (results (auth-source-search :host (concat server "/" database)
                                         :user user
                                         :port (number-to-string port)))
    (when (and (= (length results) 1)
               (plist-member (car results) :secret))
      (let ((secret (plist-get (car results) :secret)))
                (if (functionp secret) (funcall secret) secret))))))

函数做的事:接收连接参数(主机、数据库、用户、端口),把主机名和数据库名拼成 host/dbname 作为查找键,从 authinfo 文件里找到对应条目。 auth-source-search 是 auth-source 提供的查找函数。

配置 sql-mode

(setq sql-password-search-wallet-function #'my/sql-auth-source-search-wallet)
(setq sql-password-wallet "~/.authinfo.gpg")

sql-password-search-wallet-function 告诉 sql-mode "需要密码时调这个函数", sql-password-wallet 指定密码存储文件的位置。

连接定义里不写密码

(setq sql-connection-alist
  '((mydb (sql-product 'postgres)
          (sql-port 5432)
          (sql-server "db.example.com")
          (sql-user "myuser")
          (sql-database "mydb"))
    (analytics (sql-product 'postgres)
               (sql-port 5432)
               (sql-server "analytics.example.com")
               (sql-user "analyst")
               (sql-database "analytics"))))

连接定义里只有服务器、端口、用户名,没有密码。sql-mode 实际连接数据库时才从 authinfo.gpg 里查找。

特点

  • 密码存在 GPG 加密文件里,安全性好
  • 复用 Emacs 内置的 auth-source,和 TRAMP、ERC 等其他需要密码的场景共享同一套存储
  • 连接定义和密码存储分离, sql-connection-alist 里不出现明文密码
  • 需要配置 GPG 环境(gpg-agent)
  • 不限于 PostgreSQL,适合所有数据库类型

在 Org Babel 里使用

配好 sql-connection-alist 之后,在 Org Babel 的 SQL 代码块里通过 :dbconnection 参数引用连接名:

#+begin_src sql :product postgres :dbconnection mydb :results raw
SELECT current_database(), current_user;
#+end_src

:dbconnection mydb 让 Org Babel 用 sql-connection-alist 里名为 mydb 的连接去执行 SQL。Org 文件里不出现密码。

方案二在 Org Babel 里有个限制。 ob-sql (Org Babel 的 SQL 执行模块)直接从 sql-connection-alist 读取密码,不走 sql-password-search-wallet-function 。方案二的连接定义里没有密码,所以 ob-sql 执行时会因为缺少密码而认证失败。

解决办法有两种:

  1. 在初始化时从 authinfo 读取密码填入 sql-connection-alist (但这会把密码放进内存,削弱了方案二的分离优势)
  2. 同时配置 ~/.pgpass 文件,让 psql 自己找密码(psql 原生支持读取 ~/.pgpass

方案一没有这个问题,因为密码本来就在 sql-connection-alist 里。

对比

维度 pgpass 方案 authinfo.gpg 方案
密码存储 明文文件(需注意文件权限) GPG 加密文件
配置复杂度 低(一个文件 + 一个解析函数) 中(需要 GPG 环境 + 自定义查找函数)
密码在内存中 明文存在 sql-connection-alist 里 按需从 authinfo.gpg 读取
数据库类型 主要适合 PostgreSQL 系 适合所有类型
Org Babel 支持 直接可用 需要额外处理(ob-sql 不走 wallet 函数)
与 Emacs 生态集成 独立方案 复用 auth-source,可与其他包共享密码
自动重载 file-notify 监控文件变更 auth-source 自行管理缓存

只用 PostgreSQL 且对安全性要求不高,pgpass 方案简单直接,Org Babel 也开箱即用。用多种数据库或已经在用 authinfo.gpg 管理其他密码,authinfo 方案更统一,但用在 Org Babel 里需要额外配置。