zoukankan      html  css  js  c++  java
  • PostgreSQL数据库安装方法总结

    1 引言

    PostgreSQL数据库现在变得越来越流行,在DB-Engines网站(https://db-engines.com/en/ranking ),排名第四,本人所在的公司也是极力推广PG数据库,开源社区中,PG的活跃度也是非常高,本文简单介绍一下pg数据库的几种安装方法。

    2 安装

    2.1 部署规划

    本文总结了PostgreSQL数据库的三种安装方法,都是基于CentOS7.6版本来进行,可以通过/etc/redhat-release来查看具体的版本。

    [root@pgDatabase ~]# cat /etc/redhat-release 
    CentOS Linux release 7.6.1810 (Core) 
    

    对于数据库的目录规划,并没有严格的规定,但是生产环境中,还是建议将数据目录放在挂载的盘中,防止由于系统的问题导致数据库无法启动,数据丢失。

    目录 存放位置
    home目录 /home/postgres
    安装目录 /usr/local/
    数据目录 /data/pgdata

    安装文件介绍和下载链接,默认版本为pg9.6。

    安装方式 安装包名称 下载地址
    tar.gz文件解压直接安装 postgresql-9.6.13-4-linux-x64-binaries.tar.gz https://www.enterprisedb.com/download-postgresql-binaries
    源码编译安装 postgresql-9.6.10.tar.gz http://ftp.postgresql.org/pub/source/
    Rpm包安装 postgresql-server、postgresql-contrib、postgresql-libs、postgresql https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/

    2.2 解压安装

    2.2.1 创建postgres用户

    useradd postgres
    passwd postgres
    

    登陆一下,让系统创建家目录

    su - postgres
    

    2.2.2 创建目录

    mkdir -p /data/pgdata
    chown -R postgres:postgres /data/pgdata/
    chmod -R 775 /data/pgdata/
    

    2.2.3 解压安装

    tar -xzvf postgresql-9.6.13-4-linux-x64-binaries.tar.gz -C /data/
    chown -R postgres /data/pgsql
    

    解压后的文件夹名字是pgsql

    创建软连接

    cd /usr/local
    ln -s /data/pgsql pgsql
    
    

    配置环境变量

    cd /home/postgres
    vi .bash_profile
    # 在path后面添加数据库的bin目录方便启动数据库
    PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/pgsql/bin
    

    2.2.4 初始化数据库

    su - postgres
    initdb -D /data/pgdata/
    

    修改配置文件

    cd /data/pgdata/
    vi postgresql.conf
    # 将该条配置反注释,将localhost改成*
    listen_addresses = '*'
    

    修改访问控制文件

    vi pg_hba.conf
    # IPv4 local connections:
    host    all             all             0.0.0.0/0            md5
    
    

    说明:

    # METHOD can be "trust", "reject", "md5", "password", "gss", "sspi",
    # "ident", "peer", "pam", "ldap", "radius" or "cert".  Note that
    # "password" sends passwords in clear text; "md5" is preferred since
    # it sends encrypted passwords.
    
    

    关闭防火墙

    systemctl stop firewalld
    

    2.2.5 启动数据库

    [postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start
    server starting
    

    进入数据库修改postgres密码

    psql
    postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
    ALTER ROLE
    

    这样就算全部完成了,远程也可以连接数据库了。

    2.3 源码安装

    2.3.1 下载源码包

    如果服务器可以联网,可以使用wget命令来下载。或者从网站上下载然后传到服务器也可以。

    wget http://ftp.postgresql.org/pub/source/v9.6.13/postgresql-9.6.13.tar.gz
    

    2.3.2 安装依赖包

    安装前必须要保证gcc编译器已经安装好。可以使用rpm -qa gcc检查。

    yum install gcc make openssl zlib-devel -y
    
    

    2.3.3 解压安装包

    tar -xzvf postgresql-9.6.13.tar.gz -C /usr/local
    

    2.3.4 编译并安装

    cd /usr/local/postgresql-9.6.13/
    ./configure --prefix=/usr/local/pgsql
    

    问题

    ...
    configure: error: readline library not found
    If you have readline already installed, see config.log for details on the
    failure.  It is possible the compiler isn't looking in the proper directory.
    Use --without-readline to disable readline support.
    ...
    

    按照提示后面加上即可--without-readline

    ./configure --prefix=/usr/local/pgsql --without-readline
    

    安装,该过程比较长,一般在5~10分钟左右,安装完后,会在/usr/local目录下看到pgsql文件夹,也就是安装完后的数据库的位置。

    make && make install
    

    2.3.5 创建postgres用户

    useradd postgres
    passwd postgres
    

    登陆一下,让系统创建家目录

    su - postgres
    

    配置环境变量

    cd /home/postgres
    vi .bash_profile
    # 在path后面添加数据库的bin目录方便启动数据库
    PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/pgsql/bin
    

    2.3.6 创建目录

    mkdir -p /data/pgdata
    chown -R postgres:postgres /data/pgdata/
    chmod -R 700 /data/pgdata/
    

    2.3.7 初始化数据库

    su - postgres
    initdb -D /data/pgdata/
    

    输出以下内容则表明成功。

    The files belonging to this database system will be owned by user "postgres".
    This user must also own the server process.
    
    The database cluster will be initialized with locale "en_US.UTF-8".
    The default database encoding has accordingly been set to "UTF8".
    The default text search configuration will be set to "english".
    
    Data page checksums are disabled.
    
    fixing permissions on existing directory /data/pgdata ... ok
    creating subdirectories ... ok
    selecting default max_connections ... 100
    selecting default shared_buffers ... 128MB
    selecting dynamic shared memory implementation ... posix
    creating configuration files ... ok
    running bootstrap script ... ok
    performing post-bootstrap initialization ... ok
    syncing data to disk ... ok
    
    WARNING: enabling "trust" authentication for local connections
    You can change this by editing pg_hba.conf or using the option -A, or
    --auth-local and --auth-host, the next time you run initdb.
    
    Success. You can now start the database server using:
    
        pg_ctl -D /data/pgdata/ -l logfile start
    
    

    修改配置文件

    cd /data/pgdata/
    vi postgresql.conf
    # 将该条配置反注释,将localhost改成*
    listen_addresses = '*'
    

    修改访问控制文件

    vi pg_hba.conf
    # IPv4 local connections:
    host    all             all             0.0.0.0/0            md5
    
    

    说明:

    # METHOD can be "trust", "reject", "md5", "password", "gss", "sspi",
    # "ident", "peer", "pam", "ldap", "radius" or "cert".  Note that
    # "password" sends passwords in clear text; "md5" is preferred since
    # it sends encrypted passwords.
    
    

    关闭防火墙

    systemctl stop firewalld
    systemctl disable firewalld
    

    2.3.8 启动数据库

    [postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start
    server starting
    

    进入数据库修改postgres密码

    psql
    postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
    ALTER ROLE
    

    这样就算全部完成了,远程也可以连接数据库了。

    2.4 RPM安装

    2.4.1 下载rpm包

    使用rpm安装需要下载上面所列出的4个rpm包,下载的时候需要注意版本的一致。

    wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-9.6.13-1PGDG.rhel7.x86_64.rpm
    wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-libs-9.6.13-1PGDG.rhel7.x86_64.rpm
    wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-contrib-9.6.13-1PGDG.rhel7.x86_64.rpm
    wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-server-9.6.13-1PGDG.rhel7.x86_64.rpm
    
    

    2.4.2 安装依赖包

    yum install gcc make openssl zlib-devel libxslt -y
    
    

    2.4.3 安装rpm包

    注意,需要依次安装,包之间有依赖关系。

    [root@mydb ~]# rpm -ivh postgresql96-libs-9.6.13-1PGDG.rhel7.x86_64.rpm 
    warning: postgresql96-libs-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:postgresql96-libs-9.6.13-1PGDG.rh################################# [100%]
    [root@mydb ~]# rpm -ivh postgresql96-9.6.13-1PGDG.rhel7.x86_64.rpm 
    warning: postgresql96-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:postgresql96-9.6.13-1PGDG.rhel7  ################################# [100%]
    [root@mydb ~]# rpm -ivh postgresql96-server-9.6.13-1PGDG.rhel7.x86_64.rpm 
    warning: postgresql96-server-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:postgresql96-server-9.6.13-1PGDG.################################# [100%]
    [root@mydb ~]# rpm -ivh postgresql96-contrib-9.6.13-1PGDG.rhel7.x86_64.rpm 
    warning: postgresql96-contrib-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:postgresql96-contrib-9.6.13-1PGDG################################# [100%]
    
    

    2.4.4 创建目录

    mkdir -p /data/pgdata
    chown -R postgres:postgres /data/pgdata/
    chmod -R 775 /data/pgdata/
    

    配置环境变量

    cd /home/postgres
    vi .bash_profile
    # 在path后面添加数据库的bin目录方便启动数据库
    PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/pgsql-9.6/bin/
    

    2.4.5 初始化数据库

    su - postgres
    cd /usr/pgsql-9.6/bin/
    initdb -D /data/pgdata/
    

    2.4.6 配置启动数据库

    修改配置文件

    cd /data/pgdata/
    vi postgresql.conf
    # 将该条配置反注释,将localhost改成*
    listen_addresses = '*'
    

    修改访问控制文件

    vi pg_hba.conf
    # IPv4 local connections:
    host    all             all             0.0.0.0/0            md5
    
    

    说明:

    # METHOD can be "trust", "reject", "md5", "password", "gss", "sspi",
    # "ident", "peer", "pam", "ldap", "radius" or "cert".  Note that
    # "password" sends passwords in clear text; "md5" is preferred since
    # it sends encrypted passwords.
    
    

    关闭防火墙

    systemctl stop firewalld
    systemctl disable firewalld
    

    2.4.7 启动数据库

    [postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start
    server starting
    

    进入数据库修改postgres密码

    psql
    postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
    ALTER ROLE
    

    这样就算全部完成了,远程也可以连接数据库了。

    3 总结

    以上三种方法就是最常用的安装数据库的方式,当然也有更为简单的是直接yum install postgresql,但是这种方法只适合临时进行部署一个,不推荐作为生产环境,一般生产环境还会根据实际情况配置高可用的主从方式,采用流复制的方式来提高数据库可用性。

    问题:
    如果执行psql出现了
    ./psql: error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory
    解决办法:在postgres的家目录中.bashrc中添加export LD_LIBRARY_PATH=/usr/local/pgsql/lib,然后source一下就可以了。

  • 相关阅读:
    jsp获取一个对象和list对象
    request属性 request.getAttribute()
    Ajax注册表单用户名实时验证
    codeigniter 操作mysql的PHP代码--更新
    linux后台server开发环境的部署配置和验证(nginx+apache+php-fpm+FASTCGI(C/C++))
    Android
    Jquery Jqprint—随着Jquery Jqprint实现网页打印
    SQL于DML(数据库操作语言)采用
    远程数据client交换器
    如何选择项目?
  • 原文地址:https://www.cnblogs.com/easonbook/p/11005967.html
Copyright © 2011-2022 走看看