1.下载 pgsql 安装包
https://www.postgresql.org/ftp/source/
下载对应版本的安装包 Linux(postgresql-14.2.tar.gz)
2.解压 postgresql-14.2.tar.gz
tar -zxvf postgresql-14.2.tar.gz -C /opt/module
3.安装 readline-devel 库 和 zlib-devel库
yum install readline-devel(#安装readline-devel)
yum install zlib-devel(#安装zlib-devel)
4.编译pgsql
进入postgresql-14.2 文件夹
安装 C 编译环境
yum install build-essential
yum -y install gcc
5.编译postgresql源码
./configure --prefix=/pgsql/postgresql
运行命令
#运行命令
make(如果报错就gmake)
su
make install
adduser postgres
mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &
/usr/local/pgsql/bin/createdb test
/usr/local/pgsql/bin/psql test
6.配置环境变量
进入home/postgres目录可以看到.bash_profile文件。
cd /home/postgres
ls -al
vi .bash_profile
#修改的东西
export PGHOME=/pgsql/postgresql(pg的路径)
export PGDATA=/pgsql/postgresql/data(pg的data路径)
PATH=$PATH:$HOME/bin:$PGHOME/bin
#保存,退出vi。执行以下命令,使环境变量生效
source .bash_profile
7.切换用户postgres并使用initdb初使用化数据库
su - postgres
initdb (用来生成 pg 中的 data)
8.配置服务
修改/pgsql/postgresql/data目录下的两个文件。
postgresql.conf 配置PostgreSQL数据库服务器的相应的参数。
pg_hba.conf 配置对数据库的访问权限。
[postgres@weekend02 data]$ vim postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
#port = 5432 # (change requires restart)
[postgres@weekend02 data]$ vi pg_hba.conf
# IPv4 local connections:
host all all 0.0.0.0/0 trust
host all all 127.0.0.1/32 trust
9.设置PostgreSQL开机自启动
(1).PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。
linux文件即为linux系统上的启动脚本
[postgres@weekend02 pgsql]$ cd /pgsql/postgresql-11.1/contrib/start-scripts
[postgres@weekend02 start-scripts]$ ls
freebsd linux macos
(2).切换为root用户,修改linux文件属性,添加X属性
[root@weekend02 start-scripts]# chmod a+x linux
(3).复制linux文件到/etc/init.d目录下,更名为postgresql
[root@weekend02 start-scripts]# cp linux /etc/init.d/postgresql
(4).修改/etc/init.d/postgresql文件的两个变量
prefix设置为postgresql的安装路径:/pgsql/postgresql
PGDATA设置为postgresql的数据目录路径:/pgsql/postgresql/data
(5).设置postgresql服务开机自启动
[root@weekend02 init.d]# chkconfig --add postgresql
查看开机自启动服务设置成功。
[root@weekend02 init.d]# chkconfig
postgresql 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
(6).编辑/etc/sysconfig/iptables文件开放5432端口。
[root@weekend02 sysconfig]# cd /etc/sysconfig
[root@weekend02 sysconfig]# vi iptables
添加以下内容
-A INPUT -p tcp -m tcp --dport 5432 -j ACCEPT
重启服务
重启服务
[root@weekend02 sysconfig]# /etc/init.d/iptables restart
iptables:清除防火墙规则: [确定]
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:正在卸载模块: [确定]
iptables:应用防火墙规则: [确定]
(7).执行service postgresql start,启动PostgreSQL服务
[root@weekend02 init.d]# service postgresql start
Starting PostgreSQL: ok
查看PostgreSQL服务
[root@weekend02 init.d]# ps -ef | grep postgres
root 12040 3014 0 Dec15 pts/0 00:00:00 su - postgres
postgres 12041 12040 0 Dec15 pts/0 00:00:00 -bash
postgres 12177 1 0 00:29 ? 00:00:00 /pgsql/postgresql/bin/postmaster -D /pgsql/postgresql/data
postgres 12179 12177 0 00:29 ? 00:00:00 postgres: checkpointer
postgres 12180 12177 0 00:29 ? 00:00:00 postgres: background writer
postgres 12181 12177 0 00:29 ? 00:00:00 postgres: walwriter
postgres 12182 12177 0 00:29 ? 00:00:00 postgres: autovacuum launcher
postgres 12183 12177 0 00:29 ? 00:00:00 postgres: stats collector
postgres 12184 12177 0 00:29 ? 00:00:00 postgres: logical replication launcher
root 12198 12132 0 00:30 pts/0 00:00:00 grep postgres
10.测试
切换为postgres用户,进入客户端:
$ su - postgres
$ psql
创建数据库用户
赋予账号权限
新建数据库
退出
[postgres@weekend02 ~]$ psql
psql (11.1)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=# create user pg password 'pg';
CREATE ROLE
postgres=# ALTER ROLE pg SUPERUSER;
ALTER ROLE
postgres=# create database pg;
CREATE DATABASE
postgres=# \q