PostgreSQL学习笔记: 安装

软件说明

  1. 源码包 : postgresql-11.3.tar.gz
  2. 客户端 : postgresql11-11.3-1PGDG.rhel7.x86_64.rpm
  3. 附件模块 : postgresql11-contrib-11.3-1PGDG.rhel7.x86_64.rpm
  4. 库文件 : postgresql11-libs-11.3-1PGDG.rhel7.x86_64.rpm
  5. 服务器 : postgresql11-server-11.3-1PGDG.rhel7.x86_64.rpm

通过 yum 源安装

地址: PostgreSQL: Linux downloads (Red Hat family)

根据提示安装 Yum 源

yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

安装服务器和客户端

yum install postgresql11
yum install postgresql11-server

初始化数据库

/usr/pgsql-11/bin/postgresql-11-setup initdb

开启自动启动

systemctl enable postgresql-11
systemctl start postgresql-11

RPM 包直接下载

PostgreSQL RPM Repository (with Yum)

源码包安装

PostgreSQL: File Browser

下载源码,上传,并解压

tar -zxvf postgresql-11.3.tar.gz

配置编译选项

cd postgresql-11.3
./configure --help       # 查看编译选项

mkdir /home/pgdb/pgsql11
./configure --prefix=/home/pgdb/pgsql11

常用编译选项(其中 4-7 只能在编译时指定):
1. —prefix : 指定安装目录,默认 /usr/local/pgsql
2. —includedir : 指定 C 和 C++ 头文件目录,默认 <PREFIX>/include
3. —with-pgport : 指定实例的默认监听端口,默认 5432
4. —with-blocksize : 指定数据文件的块大小,默认 8K, OLAP 建议增大到 32K
5. —with-segsize : 指定单个文件的大小,默认 1GB
6. —with-wal-blocksize : 指定 WAL 文件的块大小,默认 8K
7. —with-wal-segsize : 指定单个 WAL 文件的大小,默认 16MB

编译

gmake
gmake world    # 一次性将文档及附加慕课全部编译

安装

gmake install
gmake install-world

查看版本

/home/pgdb/pgsql11/bin/postgres --version

初始化数据目录

initdb --help
initdb -D /home/pgdb/pgdata -E UTF8 -U pguser -W
or
pg_ctl -D /usr/local/pgsql/data initdb

The files belonging to this database system will be owned by user "pgdb".
This user must also own the server process.

The database cluster will be initialized with locale "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

Enter new superuser password: 
Enter it again: 

fixing permissions on existing directory /home/pgdb/pgdata ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /home/pgdb/pgdata -l logfile start

启动服务器

pg_ctl start -D /home/pgdb/pgdata -l logfile

# 下面命令也可启动
postgres -D /usr/local/pgsql/data                    # Foreground
postgres -D /usr/local/pgsql/data >logfile 2>&1 &    # Background

查看服务器状态

pg_ctl status -D /home/pgdb/pgdata
pg_isready -p 5432

关闭服务器

pg_ctl stop -D /home/pgdb/pgdata -m fast
pg_ctl stop -D /home/pgdb/pgdata -mf

kill -sigterm `head -1 /home/pgdb/pgdata/postmaster.pid`   # 不推荐使用

-m 参数指定关库的模式:
* smart : -ms, SIGTERM, 等待活动事务提交,等待客户端主动断开连接,然后关闭数据库
* fast : -mf, SIGINT, 回滚所有活动事务,强制断开客户端连接,然后关闭数据库
* immediate : -mi, SIGQUIT, 立即终止所有服务器进程,下次启动时首先进入恢复状态,不推荐使用

配置开机启动

cp /home/pgdb/postgresql-11.3/contrib/start-scripts/linux /etc/init.d/postgresql11
chmod +x postgresql11

cd /etc/init.d/
chkconfig --add postgresql11
chkconfig postgresql11 on

环境变量

export PGHOME=/home/pgdb/pgsql11
export PGDATA=/home/pgdb/pgdata   # 可以在相关命令中,不用额外指定 -D 目录参数
export PGUSER=puser
export PGPORT=5432
export LD_LIBRARY_PATH=$PGHOME/lib:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib
export MANPATH=$PGHOME/share/man:$MANPATH
export PATH=$PGHOME/bin:$PATH

服务器程序

  1. initdb : 用于创建新的数据库目录
  2. pg_archivecleanup : 清理 PG WAL 附档文件
  3. pg_controldata : 显示数据库服务器的控制信息
  4. pg_ctl : 初始化,启动,停止,控制数据库服务器的工具
  5. pg_resetwal : 清除预写日志,并有选择的重置 pg_control 中的一些信息,在控制文件损坏时最后手段
  6. pg_rewind : 在主备切换时,将原 Master 通过同步模式恢复
  7. pg_test_timing : 度量系统计时开销
  8. pg_upgrade : PG 升级工具
  9. pg_waldump : 将 WAL 日志解析为可读的格式
  10. postgres : PG 服务器程序
  11. postmaster : 指向 postgres 服务器程序的一个软连接
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容