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
相关推荐
c++逐梦人1 小时前
Linux下编写进度条小程序
linux·运维·小程序
求知若渴,虚心若愚。1 小时前
traefik 启用并指定根证书*.cer
linux·运维·服务器
n***27192 小时前
SQL Server 中行转列
运维·服务器
q***82912 小时前
Nginx中$http_host、$host、$proxy_host的区别
运维·nginx·http
Claudedy2 小时前
Linux 网络代理指南:解决下载慢、访问受限的开发痛点
linux·运维·网络·代理·proxy代理
q***14642 小时前
RustDesk搭建公网中继服务器远控内网机器(完整版)
运维·服务器
zhaqonianzhu2 小时前
【保姆级】无外网 Linux 服务器用 VSCode 通义灵码:SSH 代理配置全流程
linux·服务器·vscode
Murphy_lx2 小时前
C++ 条件变量
linux·开发语言·c++
EasyCVR3 小时前
视频汇聚平台EasyCVR服务器使用WiFi网卡时,为何无法向级联平台发送注册?
运维·服务器