Skip to content

Postgres sql 安装

安装步骤

安装参考

异常问题参考

sh
# 解压
tar -zxvf postgres.tar.gz
# 切换到解压目录
cd xxxx
# 编译
./configure
# 如果会报错 执行
./configure --without-readline
# 安装
make
make install

postgres安装目录

程序运行

sh
# 创建postgres/postgres用户
useradd -s /bin/bash -d /home/postgres -m postgres
# 创建用户组
sudo usermod -aG postgres postgres
# 创建包目录
mkdir -p /export/pgdata 
# 目录对postgres用户授权
chown -R postgres:postgres /export/pgdata 
# 切换用户
sudo su - postgres
# 初始化数据库
initdb -D /export/pgdata
# 如果报initdb未找到命令,临时?
# 查看环境变量
echo $PATH 
# 把pgsql的bin加入环境变量
export PATH=/usr/local/pgsql/bin:$PATH


# 服务启动
pg_ctl -D /export/pgdata/ -l logfile start
# 测试
psql

建库

postgres建库

初始化数据库

初始化数据库

启动/测试

postgres启动

程序自启动

sh
# 创建postgres 服务
sudo vim /etc/systemd/system/postgres.service


# 贴入
[Unit]
Description=PostgreSQL database server
After=network.target


[Service]
Type=forking
# 用户/用户组
User=postgres
Group=postgres


# Location of the database's bin directory
Environment=PGHOME=/usr/local/pgsql
Environment=PGDATA=/export/pgdata


# Where to send early-startup messages from the server (before the logging sortes out)
# This is normally /var/log/postgresql/, but the server may not have permissions to
# create that directory before the logging subsystem starts.
Environment=PGLOG=/var/tmp/postgres.log
# 可能会报权限不足,无法启动,则使用下面的
# ExecStart=/usr/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
# ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecStart=/usr/local/pgsql/bin/pg_ctl start -D ${PGDATA} -t 300
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D ${PGDATA} -m fast
# Give a grace period of 60 seconds to allow the server to shutdown cleanly
TimeoutSec=60


[Install]
WantedBy=multi-user.target
sh
# 重新加载systemd管理器配置:
sudo systemctl daemon-reload
# 启动postgres服务
sudo systemctl start postgres
# 关闭postgres服务
sudo systemctl stop postgres
# 重启postgres服务
sudo systemctl restart postgres
# postgres设置为开机自启。
sudo systemctl enable postgres
# 取消postgres开机自启。
sudo systemctl disable postgres
# 检查postgres服务状态。
sudo systemctl status postgres

端口/IP 配置

配置文件地址

/export/pgdata/postgresql.conf

postgres修改配置1

修改内容

postgres修改配置2

sh
# 修改端口,删除port前注释#符号
port = 指定端口
# 修改监听ip,删除listen_addresses前注释#符号
listen_addresses = "*"

如果连接时报FATAL: no pg_hba.conf entry for host "", user "", database "***", no encryption

postgres修改配置3

修改配置文件

/export/pgdata/pg_hba.conf

sh
host    all             账户        对应IP/32            trust

PostGis 安装

参考文档

sh
# 解压
tar -zxvf postgis.tar.gz
# 进入解压目录
cd  /xxx
# 编译
./configure
# 安装
make && make install