PostgreSQL安装部署教程!

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.servicesystemctl disable firewalld.service​​​​​​​
关闭selinuxsed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/configsetenforce 0​​​​​​​
设置时区同步时间rm -rf /etc/localtimels -n /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeyum install ntpdate -yntpdate time1.aliyun.comhwclock -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 -yyum 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}​​​​​​​
编译postgresqltar -xf postgresql-17.5.tar.gzcd 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-ldapgmake worldgmake install-world​​​​​​​
创建运行用户groupadd postgresuseradd -g postgres postgreschown -R postgres.postgres /data/pgsqlchmod 700 /data/pgsql/logs

3.3、配置环境变量​​​​​​​

cat >> /etc/profile <<EOFexport PGHOME=/data/pgsqlexport PGDATA=$PGHOME/dataexport PATH=$PATH:$PGHOME/binexport LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATHEOF 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/bashPG_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>&1if [ $? -eq 0 ]; thenecho "PostgreSQL started successfully."elseecho "Failed to start PostgreSQL."fi}stop_postgresql() {echo "Stopping PostgreSQL..."${PG_BIN}/pg_ctl -D ${PG_DATA} stop -l ${PG_LOG} > /dev/null 2>&1if [ $? -eq 0 ]; thenecho "PostgreSQL stopped successfully."elseecho "Failed to stop PostgreSQL."fi}restart_postgresql() {echo "Restarting PostgreSQL..."${PG_BIN}/pg_ctl -D ${PG_DATA} restart -l ${PG_LOG} > /dev/null 2>&1if [ $? -eq 0 ]; thenecho "PostgreSQL restarted successfully."elseecho "Failed to restart PostgreSQL."fi}check_postgresql_status() {${PG_BIN}/pg_ctl -D ${PG_DATA} status -l ${PG_LOG} > /dev/null 2>&1if [ $? -eq 0 ]; thenecho "PostgreSQL is running."elseecho "PostgreSQL is not running."fi}case $1 instart)start_postgresql;;stop)stop_postgresql;;restart)restart_postgresql;;status)check_postgresql_status;;*)echo "Usage: $0 {start|stop|restart|status}"exit 1;;esacEOF​​​​​​​
chmod +x /data/pgsql/pgsql.shsh /data/pgsql/pgsql.sh restartsh /data/pgsql/pgsql.sh statussh /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=postgresEnvironment=PGDATA=/data/pgsql/dataEnvironment=PGHOME=/data/pgsqlExecStart=/data/pgsql/bin/pg_ctl start -D ${PGDATA}ExecStop=/data/pgsql/bin/pg_ctl stopExecReload=/data/pgsql/bin/pg_ctl reload TimeoutSec=300  StandardOutput=syslog  StandardError=syslogSyslogIdentifier=postgres [Install]WantedBy=multi-user.targetEOF​​​​​​​
systemctl daemon-reloadsystemctl start postgresqlsystemctl enable postgresqlsystemctl restart postgresqlsystemctl status postgresql

4、进入postgresql控制台​​​​​​​

su - postgrespsql -U postgres -d postgres -h 127.0.0.1 -p 5432ALTER USER postgres WITH PASSWORD 'postgres';select datname from pg_database;\q

5、修改配置文件​​​​​​​

cat >/data/pgsql/data/postgresql.conf <<EOFport = 5432max_connections = 1000listen_addresses = '*'log_destination = 'csvlog'logging_collector = onlog_directory = '/data/pgsql/logs/'log_filename = 'postgresql-%d.log'log_file_mode = 0600log_rotation_age = 1dlog_rotation_size = 1024MBlog_truncate_on_rotation = onlog_lock_waits = offlog_statement = 'none'log_duration = onlog_min_duration_statement = 0log_connections = offlog_disconnections = offlog_line_prefix = '%m [%p] %u %d %r'log_timezone = 'Asia/Shanghai'EOF​​​​​​​
cat >>/data/pgsql/data/pg_hba.conf <<EOFhost all all 127.0.0.1/32 trusthost all all 0.0.0.0/0 md5EOF

systemctl restart postgresql

6、总结

PostgreSQL是一款开源的高度可扩展、跨平台的关系型数据库管理系统,支持复杂数据类型、ACID事务、触发器、存储过程等特性,具备强大的查询优化器和完整性约束,拥有庞大的全球社区支持。它适用于各种规模和类型的应用,提供稳定可靠的数据库解决方案。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值