"""Mac OS 上安装 PostgreSQL 离线实验:校验 runpsql.sh 启动参数、macOS 安装目录布局、pgAdmin 4 连接项及本地 SQL 冒烟测试。"""
import json
import platform
import re
import sqlite3
def run() -> dict:
cases = []
# Case 1: Parse runpsql.sh prompt defaults
transcript = """
$ /Library/PostgreSQL/11/scripts/runpsql.sh ;exit
Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]:
"""
defaults = dict(re.findall(r"^(Server|Database|Port|Username) \[([^\]]+)\]:", transcript, re.M))
exp1 = {
"Server": "localhost",
"Database": "postgres",
"Port": "5432",
"Username": "postgres",
}
act1 = defaults
cases.append(
{
"name": "macos_runpsql_script_parameters",
"summary": "解析 macOS 下 runpsql.sh 交互式启动脚本的四项默认连接参数",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
}
)
# Case 2: macOS DMG wizard steps & directory layout
base = "/Library/PostgreSQL/16"
layout = {
"bin": f"{base}/bin",
"data": f"{base}/data",
"scripts": f"{base}/scripts/runpsql.sh",
}
wizard_steps = ["dmg_mount", "admin_auth", "install_dir", "components", "data_dir", "password_port", "finish"]
exp2 = {
"wizard_step_count": 7,
"has_runpsql": True,
"bin_prefix_ok": True,
}
act2 = {
"wizard_step_count": len(wizard_steps),
"has_runpsql": layout["scripts"].endswith("/scripts/runpsql.sh"),
"bin_prefix_ok": layout["bin"].startswith("/Library/PostgreSQL/"),
}
cases.append(
{
"name": "macos_dmg_wizard_and_install_layout",
"summary": "校验 macOS DMG 图形向导步骤与 /Library/PostgreSQL 目录结构",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
}
)
# Case 3: pgAdmin 4 macOS launcher & server connection profile
pgadmin_profile = {
"launch_methods": ["Applications/pgAdmin 4.app", "menu_bar_elephant_icon"],
"server_tree": ["Servers", "PostgreSQL"],
"default_port": 5432,
}
exp3 = {"launch_method_count": 2, "root_node": "Servers", "default_port": 5432}
act3 = {
"launch_method_count": len(pgadmin_profile["launch_methods"]),
"root_node": pgadmin_profile["server_tree"][0],
"default_port": pgadmin_profile["default_port"],
}
cases.append(
{
"name": "macos_pgadmin4_connection_profile",
"summary": "验证 macOS 下启动 pgAdmin 4 与展开 Servers 节点连接本地服务的配置模型",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
}
)
# Case 4: Initial SQL smoke test in memory SQLite
conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE mac_check (id INTEGER PRIMARY KEY, client TEXT NOT NULL);")
conn.execute("INSERT INTO mac_check VALUES (1, 'runpsql.sh'), (2, 'pgAdmin 4');")
rows = conn.execute("SELECT id, client FROM mac_check ORDER BY id;").fetchall()
conn.close()
exp4 = [[1, "runpsql.sh"], [2, "pgAdmin 4"]]
act4 = [[r[0], r[1]] for r in rows]
cases.append(
{
"name": "macos_initial_sql_connectivity_smoke",
"summary": "通过内存 SQLite 验证安装后的基础建表与查询执行链路",
"expected": exp4,
"actual": act4,
"passed": act4 == exp4,
}
)
return {
"version": f"CPython {platform.python_version()} / SQLite {sqlite3.sqlite_version} (offline PostgreSQL verifier)",
"cases": cases,
}
if __name__ == "__main__":
print(json.dumps(run(), ensure_ascii=False, indent=2))
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/postgresql-mac-install-postgresql-lab/explore-mac-install-postgresql.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-mac-install-postgresql.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1655.zip
课程关联工程包(登记关联45章) · 含当前章节或实例 · mbskycom202612a1633.zip