postgres安装

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了postgres安装相关的知识,希望对你有一定的参考价值。

一. 源码安装

规划信息
安装目录: /opt/postgresql
用户/用户组:postgres/postgres 用户家目录/postgres
数据目录: /postgres/data
日志文件:/postgres/postgres.log
端口:5432

1. 获取源码

 wget https://ftp.postgresql.org/pub/source/v11.2/postgresql-11.2.tar.gz

2. 编译安装

# 安装编译器,make版本3.8以上
yum -y install gcc make
# readline和zlib是必须
yum -y install readline readline-devel zlib zlib-devel openssl openssl-devel

# 解压包
tar -xf postgresql-11.2.tar.gz 
cd postgresql-11.2/
# 编译安装
./configure --prefix=/opt/postgresql --with-openssl
make
# 编译手册
# make world
make install
# 安装手册
# make install-world

# 添加共享库到系统
echo ‘/opt/postgresql/lib‘ > /etc/ld.so.conf.d/postgresql.conf
ldconfig
ldconfig -p | grep postgresql 
# 添加到环境变量
echo ‘PATH=/opt/postgresql/bin:$PATH‘ >> /etc/profile.d/app.sh
echo ‘export PATH‘ >> /etc/profile.d/app.sh 
source /etc/profile

3. 初始化并启动服务

# 创建用户
useradd -d /postgres postgres
# 更改目录所属权限
chown -R postgres: /opt/postgresql /postgres 
# 初始化数据
su - postgres -c ‘initdb -D /postgres/data‘

# 运行这个就可以启动数据库 su - postgres  -c ‘pg_ctl -D /postgres/data -l /postgres/postgres.log start‘
# 推荐使用服务单元启动,编写服务单元。(下面$前面加反斜杠,这样输入到文件就会输入$符号)
cat > /usr/lib/systemd/system/postgresql.service <<EOF
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
Environment=PGPORT=5432
Environment=PGDATA=/postgres/data
Environment=PGLOG=/postgres/postgres.log
# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
# ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA}
ExecStart=/opt/postgresql/bin/pg_ctl start -D ${PGDATA} -s -l ${PGLOG} -o "-p ${PGPORT}" -w -t 300
ExecStop=/opt/postgresql/bin/pg_ctl stop -D ${PGDATA} -s -l ${PGLOG} -m fast
ExecReload=/opt/postgresql/bin/pg_ctl reload -D ${PGDATA} -s -l ${PGLOG}
TimeoutSec=300
[Install]
WantedBy=multi-user.target

EOF
# 添加到开机自启
systemctl enable postgresql.service
# 启动服务
systemctl start postgresql.service

二. yum安装

1. 安装源

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

2. 安装服务

# 安装客户端
yum install postgresql
# 安装服务端
yum install postgresql-server

# 查看服务启动脚本
rpm -ql postgresql-server | grep service
# 服务脚本:/usr/lib/systemd/system/postgresql.service

服务脚本中指定一些参数,可以修改(但是要注意selinux,selinux关闭可以随便修改,没有关闭,要添加到selinux策略中)

数据目录: /var/lib/pgsql/data
端口:5432
用户/用户组:postgres/postgres
可以添加 -l 指定log日志

3. 初始化并启动服务

# 初始化数据库 默认初始化库为服务脚本中指定的路径/var/lib/pgsql/data
# 备注: /usr/bin/postgresql-setup 是一个shell脚本
postgresql-setup initdb
# 添加到开机自启
systemctl enable postgresql.service
# 启动服务
systemctl start postgresql.service

以上是关于postgres安装的主要内容,如果未能解决你的问题,请参考以下文章

PHP代码-psysh调试代码片段工具

postgres安装

VIM 代码片段插件 ultisnips 使用教程

无法在 MACOS mojave 上运行来自 python 2.7 的 postgres 请求

postgres安装

Linux(CentOS 7)下安装postgres