PostgreSQL 数据库进阶

2025年10月05日
阅读时长约 3 分钟
PostgreSQL 数据库进阶

内容摘要

深入学习PostgreSQL高级特性,包括索引优化、查询性能调优、事务管理、备份恢复等企业级应用。

1. 安装 PostgreSQL

Ubuntu / Debian
sudo apt install postgresql postgresql-contrib
macOS
brew install postgresql

2. 连接数据库

常见操作
psql -U postgres
\l      -- 列出数据库
\c mydb -- 连接数据库
\dt     -- 列出表

3. 性能优化

创建索引
CREATE INDEX idx_user_email ON users(email);
分析查询性能
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'test@test.com';

4. 备份与恢复

备份
pg_dump mydb > backup.sql
恢复
psql mydb < backup.sql