Linux下PostgreSQL-12.0安装部署详细步骤

一、安装环境
  • postgresql-12.0

  • CentOS-7.6

  • 注意:确认linux系统可以正常连接网络,因为在后面需要添加依赖包。

二、pg数据库安装包下载

下载地址:PostgreSQL: File Browser

选择要安装的版本进行下载:

三、安装依赖包

在要安装postgresql数据库的Linux服务器上执行以下命令安装所需要的依赖包:
*

复制代码
    yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake
四、安装postgres

1、在根目录下新建/opt/pgsql文件夹,并将pgsql的压缩包移入。

  • 2、解压压缩包

复制代码
    tar -zxvf postgresql-12.0.tar.gz 

3、进入解压后的文件夹
*

复制代码
    cd postgresql-12.0

4、编译postgresql源码
*

复制代码
    ./configure --prefix=/opt/pgsql/postgresql

    make

    make install

至此,已完成postgreql的安装。进入/opt/pgsql/postgresql目录可以看到安装后的postgresql的文件。
*

五、创建用户组postgres并创建用户postgres
复制代码
    groupadd postgres

    useradd -g postgres postgres

    id postgres


*

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

复制代码
    mkdir data

    chown postgres:postgres data


*

七、配置环境变量

进入home/postgres目录可以看到.bash_profile文件。
*

复制代码
    cd /home/postgres

    ls -al

编辑修改.bash_profile文件。
*

复制代码
    vim .bash_profile 

添加以下内容。
*

复制代码
    export PGHOME=/opt/pgsql/postgresql
     
    export PGDATA=/opt/pgsql/postgresql/data
     
    PATH=$PATH:$HOME/bin:$PGHOME/bin

保存,退出vim。执行以下命令,使环境变量生效
*

复制代码
    source .bash_profile 
八、切换用户到postgres并使用initdb初使用化数据库
复制代码
    su - postgres
复制代码
initdb

可以看到/opt/pgsql/postgresql/data已经有文件了。

复制代码
cd /opt/pgsql/postgresql/data
九、配置服务

修改/opt/pgsql/postgresql/data目录下的两个文件。

postgresql.conf 配置PostgreSQL数据库服务器的相应的参数。

pg_hba.conf 配置对数据库的访问权限。

复制代码
vim postgresql.conf 

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

复制代码
vim pg_hba.conf

host    all            all             0.0.0.0/0               trust
#新增这一行

找到最下面这一行,这样局域网的人才能访问

十、设置PostgreSQL开机自启动

PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。

linux文件即为linux系统上的启动脚本

复制代码
cd /opt/pgsql/postgresql-12.0/contrib/start-scripts

切换为root用户,修改linux文件属性,添加X属性

复制代码
su root
复制代码
chmod a+x linux

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

复制代码
cp linux /etc/init.d/postgresql

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

prefix设置为postgresql的安装路径:/pgsql/postgresql

PGDATA设置为postgresql的数据目录路径:/pgsql/postgresql/data

复制代码
vim /etc/init.d/postgresql 

设置postgresql服务开机自启动

复制代码
chkconfig --add postgresql

执行service postgresql start,启动PostgreSQL服务

复制代码
service postgresql start
相关推荐
chlk1231 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑1 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件1 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
碳基沙盒1 天前
OpenClaw 多 Agent 配置实战指南
运维
深紫色的三北六号1 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash2 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI2 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行3 天前
Linux和window共享文件夹
linux
木心月转码ing3 天前
WSL+Cpp开发环境配置
linux
蝎子莱莱爱打怪4 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes