1、postgresql简介
1.起源与发展
PostgreSQL起源于1986年加州大学伯克利分校的POSTGRES项目,由Michael Stonebraker教授领导开发。最初目标是构建一个支持复杂数据模型和查询的数据库系统。1994年,项目更名为Postgres95,首次引入SQL支持;1996年正式定名PostgreSQL,成为开源社区中功能最强大的对象-关系型数据库管理系统(ORDBMS)之一。其名称中的 "SQL" 强调了对SQL标准的兼容性,而"Post"则延续了伯克利项目的基因。
2.核心特性
开源与灵活性:基于BSD许可证,允许免费使用、修改和分发,适用于商业、学术等多种场景。
功能完备性:
支持复杂查询、多版本并发控制(MVCC)、事务完整性、外键约束、触发器等。
提供丰富的扩展能力,允许用户自定义数据类型、操作符、索引方法及过程语言(如PL/pgSQL、Python等)。
内置JSON/JSONB、GIS(通过 PostGIS 扩展)、全文检索等高级功能。
高可靠性:通过WAL(预写式日志)和流复制技术保障数据安全,支持主从复制、逻辑复制及分布式部署。
性能优化:支持并行查询、索引快速扫描(Index-Only Scans)、SIMD加速等,在OLTP和OLAP场景均有优异表现。
3.应用场景
Web应用:作为后端数据库存储用户、订单等结构化数据,支持高并发读写。
地理信息系统(GIS):通过 PostGIS 扩展实现空间数据存储与分析,广泛用于地图服务和位置智能。
大数据与金融:支持复杂聚合查询与事务处理,适用于金融交易系统和大规模数据分析。
科学研究:管理实验数据、模拟结果,兼容多种科学计算工具链。
4.生态系统与扩展
PostGIS:空间数据库扩展,提供地理对象支持及空间索引,是开源GIS的核心组件。
云服务集成:如腾讯云PostgreSQL(支持高可用、无服务器架构及分布式部署)。
开发者工具:提供psql命令行工具、pgAdmin图形界面及多种语言驱动(Python、Java 等)。
5.最新发展(截至 2025 年 5 月)
PostgreSQL 17.5/16.9:2025年5月发布,重点增强安全性与性能:
新增参数restrict_nonsystem_relation_kind防御SQL注入攻击。
优化查询计划器与逻辑复制稳定性,修复内存泄漏问题。
支持CPU SIMD加速,提升JSON处理效率。
PostgreSQL18 Beta:引入多核并行索引构建、增强AI驱动的查询优化等前瞻特性。
2、系统初始化
关闭firewalld防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
关闭selinux
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
setenforce 0
设置时区同步时间
rm -rf /etc/localtime
ls -n /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
yum install ntpdate -y
ntpdate time1.aliyun.com
hwclock -w
修改主机名称
hostnamectl set-hostname pgsql && bash
安装依赖包
yum -y install bison tcl tcl-devel uuid-devel perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel libicu-devel openldap-devel python3-devel gcc-c++ openssl-devel cmake gcc* readline-devel
yum -y install docbook-dtds docbook-style-xsl openjade libxml2 libxml2-devel libxslt libxslt-devel xmlto fop perl-XML-SAX
yum install readline-devel zlib-devel openssl-devel -y
yum install perl-ExtUtils-Embed perl-ExtUtils-MakeMaker -y
3、安装postgresql数据库
3.1、下载postgresql
wget https://ftp.postgresql.org/pub/source/v17.5/postgresql-17.5.tar.gz
3.2、安装postgresql
创建目录
mkdir -p /data/pgsql/{data,logs}
编译postgresql
tar -xf postgresql-17.5.tar.gz
cd postgresql-17.5
./configure --prefix=/data/pgsql --with-openssl --with-pgport=5432 --with-tcl --with-perl --with-python --with-libxml --with-libxslt --with-ossp-uuid --with-pam --with-ldap
gmake world
gmake install-world
创建运行用户
groupadd postgres
useradd -g postgres postgres
chown -R postgres.postgres /data/pgsql
chmod 700 /data/pgsql/logs
3.3、配置环境变量
cat >> /etc/profile <<EOF
export PGHOME=/data/pgsql
export PGDATA=$PGHOME/data
export PATH=$PATH:$PGHOME/bin
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
EOF
source /etc/profile
3.4、初始化数据
su - postgres
/data/pgsql/bin/initdb -D /data/pgsql/data --encoding=UTF8 --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8
/data/pgsql/bin/pg_ctl -D /data/pgsql/data -l logfile start
/data/pgsql/bin/pg_ctl -D /data/pgsql/data -l logfile stop
/data/pgsql/bin/pg_ctl -D /data/pgsql/data -l logfile restart
/data/pgsql/bin/pg_ctl -D /data/pgsql/data -l logfile status
3.5、设置开机启动
cat >/data/pgsql/pgsql.sh <<EOF
#!/bin/bash
PG_BIN="/data/pgsql/bin"
PG_DATA="/data/pgsql/data"
PG_LOG="/data/pgsql/logs/pg_server.log"
start_postgresql() {
echo "Starting PostgreSQL..."
${PG_BIN}/pg_ctl -D ${PG_DATA} start -l ${PG_LOG} > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "PostgreSQL started successfully."
else
echo "Failed to start PostgreSQL."
fi
}
stop_postgresql() {
echo "Stopping PostgreSQL..."
${PG_BIN}/pg_ctl -D ${PG_DATA} stop -l ${PG_LOG} > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "PostgreSQL stopped successfully."
else
echo "Failed to stop PostgreSQL."
fi
}
restart_postgresql() {
echo "Restarting PostgreSQL..."
${PG_BIN}/pg_ctl -D ${PG_DATA} restart -l ${PG_LOG} > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "PostgreSQL restarted successfully."
else
echo "Failed to restart PostgreSQL."
fi
}
check_postgresql_status() {
${PG_BIN}/pg_ctl -D ${PG_DATA} status -l ${PG_LOG} > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "PostgreSQL is running."
else
echo "PostgreSQL is not running."
fi
}
case $1 in
start)
start_postgresql
;;
stop)
stop_postgresql
;;
restart)
restart_postgresql
;;
status)
check_postgresql_status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
EOF
chmod +x /data/pgsql/pgsql.sh
sh /data/pgsql/pgsql.sh restart
sh /data/pgsql/pgsql.sh status
sh /data/pgsql/pgsql.sh stop
3.6、配置systemd启动
cat >/usr/lib/systemd/system/postgresql.service <<EOF
[Unit]
Description=PostgreSQL Database Server
After=network.target syslog.target
[Service]
Type=forking
User=postgres
Group=postgres
Environment=PGDATA=/data/pgsql/data
Environment=PGHOME=/data/pgsql
ExecStart=/data/pgsql/bin/pg_ctl start -D ${PGDATA}
ExecStop=/data/pgsql/bin/pg_ctl stop
ExecReload=/data/pgsql/bin/pg_ctl reload
TimeoutSec=300
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=postgres
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start postgresql
systemctl enable postgresql
systemctl restart postgresql
systemctl status postgresql
4、进入postgresql控制台
su - postgres
psql -U postgres -d postgres -h 127.0.0.1 -p 5432
ALTER USER postgres WITH PASSWORD 'postgres';
select datname from pg_database;
\q
5、修改配置文件
cat >/data/pgsql/data/postgresql.conf <<EOF
port = 5432
max_connections = 1000
listen_addresses = '*'
log_destination = 'csvlog'
logging_collector = on
log_directory = '/data/pgsql/logs/'
log_filename = 'postgresql-%d.log'
log_file_mode = 0600
log_rotation_age = 1d
log_rotation_size = 1024MB
log_truncate_on_rotation = on
log_lock_waits = off
log_statement = 'none'
log_duration = on
log_min_duration_statement = 0
log_connections = off
log_disconnections = off
log_line_prefix = '%m [%p] %u %d %r'
log_timezone = 'Asia/Shanghai'
EOF
cat >>/data/pgsql/data/pg_hba.conf <<EOF
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
EOF
systemctl restart postgresql
6、总结
PostgreSQL是一款开源的高度可扩展、跨平台的关系型数据库管理系统,支持复杂数据类型、ACID事务、触发器、存储过程等特性,具备强大的查询优化器和完整性约束,拥有庞大的全球社区支持。它适用于各种规模和类型的应用,提供稳定可靠的数据库解决方案。