PostgreSQL 离线安装,源码安装,tar.gz安装

一 、 准备

1.安装环境 :CentOS-7.6

2.Pg安装包下载地址:http://www.postgresql.org/ftp/source/

3.

4.安装方法:离线安装

二、安装

1). 在root权限下创建用户组 postgres 并创建用户postgres 

Demo:

```

[root@localhost postgresql-12.0]# groupadd postgres

[root@localhost postgresql-12.0]# useradd -g postgres postgres

[root@localhost postgresql-12.0]# password postgres

```

设置linux系统中postgres用户的密码。

2) .创建postgreSQL的数据存储目录,这个需要根据磁盘的空间大小选择(可以使用df -hT 来看)。我这里放置在/pgsql下面测试。

[root@localhost ~]mkdir /pgsql

[root@localhost ~] chown -R postgres:postgres   /pgsql

3)使用postgres用户重新登录并上传安装包。

将postgresql-12.0.tar.gz上传至/home/postgres下。

4).解压压缩包

[ postgres @localhost ~]#tar -zxvf postgresql-12.0.tar.gz 

5)进入解压后的文件夹 

[ postgres@localhost ~]#cd postgresql-12.0 

6)编译 postgresql 源码

[ postgres@localhost postgresql-12.0]# ./configure --prefix=/pgsql

在编译过程中可能会报错,请根据提示下载相关依赖包

注: 根据Centos的版本下载rpm依赖包并安装。因每个系统的版本和系统安装模式不一样,缺少的包也不一致,无法做到问题的统一解决。

rpm   -ivh   xxx.rpm                        # rpm包的安装指令。缺少的安装包需要自行下载并安装!(安装时需要root权限,需要先切过去)

注:可以去下面这些网址搜索缺少的rpm包。

http://rpm.pbone.net/

http://www.rpmfind.net/linux/rpm2html/search.php?query

https://pkgs.org/

如果在安装缺包后,依然报上述错误,请在 ./configure --prefix=/pgsql  后加上 --without-zlib 的指令。(以zlib为例)

[postgres@localhost postgresql-12.0]# make     

[postgres@localhost postgresql-12.0]# make install

[postgres@localhost~]# cd /pgsql

[postgres@localhost ~]# ls -al

bin  

include  

lib  

share

7).    创建 postgresql 数据库的数据主目录并修改文件所有者这个数据库主目录是随实际情况而不同,这里我们的主目录是在/pgsql /data 目录下:

[ postgres@localhost pgsql]# mkdir data

[postgres@localhost pgsql]# ls -al

8). 配置环境变量 进入/home/postgres 目录可以看到.bash_profile 文件。

[ postgres@localhost pgsql]# cd ~

[ postgres@localhost]# vi .bash_profile 

添加以下内容。

export PGHOME=/pgsql

export PGDATA=/pgsql/data

PATH=$PATH:$HOME/bin:$PGHOME/bin

[ postgres@localhost ]# source .bash_profile 

9.)    切换用户到 postgres 并使用 initdb 初使用化数据库

[postgres@localhost ~]# initdb 

10).  配置服务

[postgres@localhost]$ cd /pgsql/postgresql/data

[postgres@localhost ]$ vi postgresql.conf 

listen_addresses= '*'                       #允许所有的IP连接PG     

port= 5432                                          #开放5432端口           

注: 生产环境中PG的参数还需要另行优化。此处省略,如需要交流请联系博主。

注:其中,参数“listen_addresses”表示监听的 IP 地址,默认是在 localhost 处监听,也就是 127.0.0.1 的 ip 地址上监听,只接受来自本机 localhost 的连接请求,这会让远程的主机无法登陆这台数据库,如果想从其他的机器上登陆这台数据库,需要把监听地址改为实际网络的地址,一种简单的方法是,将行开头的#去掉,把这个地址改为*,表示在本地的所有地址上监听。 

[postgres@localhost data]$ vi pg_hba.conf 

在最后一行 ,添加如下内容。(所有IP均可以登录PG且需要做md5验证登录)

host    all             all             0.0.0.0/0                 md5

11). 设置 PostgreSQL 开机自启动。 PostgreSQL 的开机自启动脚本位于contrib/start-scripts 路径下,linux 文件即为 linux 系统上的启动脚本 (root权限

[root@localhost]$ cd /home/postgres/postgresql-12.0/contrib/start-scripts

修改 linux 文件属性,添加 X 可执行权限

[root@localhost start-scripts]# chmod a+x linux

12) 复制 linux 文件到/etc/init.d 目录下,更名为postgresql

[root@localhost start-scripts]# cp linux  /etc/init.d/postgresql

修改/etc/init.d/postgresql 文件的两个变量 prefix 和PGDATA

[root@localhost ] $ vi /etc/init.d/postgresql

修改为 自己的安装路径:

prefix=/pgsql

PGDATA="/pgsql/data"

设置 postgresql 服务开机自启动: 

[root@localhost ~]#   cd /etc/init.d

[root@localhost init.d]# chkconfig --add postgresql

[root@localhost init.d]# chkconfig  

查看开机自启动服务设置成功。

postgresql    0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭

13)执行 service

postgresql start,启动 PostgreSQL 服务

[root@localhost init.d]# service postgresql  start

Starting PostgreSQL: ok   # 表示启动成功

现在就可以通过客户端进行连接访问数据库!  

14)验证

切换用户:    

[root@localhost ~]#  su - postgres  

[ postgres@localhost ~]#  psql                     ## (如果失败再次执行source .bash_profile)

postgres=#  ALTER USER postgres WITH PASSWORD '你的密码';

修改postgres的密码

最后可通过远端可视化工具登录测试。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容