zoukankan      html  css  js  c++  java
  • PostgreSQL数据库的安装与配置

    项目中要用PostgreSQL,所以专门学习了一下如何安装和调试postgresql,此博文用来记录自己的安装和调试过程,安装环境是centos7。

    首先尝试了yum安装,因为毕竟yum安装简单粗暴,官网的安装指导地址:

    https://www.postgresql.org/download/linux/redhat/

    安装了半天,报错,以为是自己的yum源有问题,换了几个,还是不行,所以只能放弃这种安装方式。

    接下来尝试源码编译安装,我们首先从官网取Source,官网在这里:

    https://www.postgresql.org/ftp/source/

    打开如下:

    目前已经最新的版本已经到10了,但最新的稳定版是9.6,所以我下载就是9.6这个版本,执行下面命令下载:

    wget https://www.postgresql.org/ftp/source/v9.6.2/
    

    取到source后,准备开始编译安装。

    这里需要注意的postgresql编译需要预装一些工具。除了make、gcc(GNU编译器套件)这些基本必备的工具,还要有zlib、bison等等,也不用提前装,反正安装过程中缺少什么再装就好了。

    取到源码,先解压:

    tar -zxvf postgresql-9.6.2.tar.gz
    

    进入postgresql-9.6.2文件夹:

    再执行下面指令:

    export CFLAGS = "-g -Wall -Wmissing-prototypes -Wpointer-arith 
    -Wdeclaration-after-statement -Wendif-labels -Wformat-security 
    -fno-strict-aliasing -fwrapv"
    

    然后再执行下列指令

    ./configure --prefix=/opt/psql --with-perl --with-tcl --with-python --with-openssl 
    --with-pam --without-ldap --with-libxml --with-libxslt --enable-thread-safety 
    --with-wal-blocksize=16 --with-blocksize=16 --enable-dtrace --enable-debug  
    

    其中--prefix是指定软件的安装路径,--with选项是指安装本文件依赖的库文件。如有不清楚可以自己学习下configure命令的相关参数。

    运行之后,会出现报错,我这个报的错还挺多,我都贴上来,供大家参考一下:

    问题1:  
    checking for dtrace... no  
    configure: error: dtrace not found  
    解决方法:  
    yum search dtrace  
    Loaded plugins: fastestmirror, refresh-packagekit, security  
    Loading mirror speeds from cached hostfile  
     * base: mirrors.163.com  
     * extras: mirrors.163.com  
     * updates: mirrors.163.com  
    =============================================================================================== Matched: dtrace ===============================================================================================  
    systemtap-sdt-devel.i686 : Static probe support tools  
    systemtap-sdt-devel.x86_64 : Static probe support tools  
      
    找到了,就安装,我是64位的,安装第二个  
    [root@localhost postgresql-9.3.5]# yum install -y systemtap-sdt-devel.x86_64  
    
    问题2:  
    checking for flags to link embedded Perl... Can't locate ExtUtils/Embed.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).  
    BEGIN failed--compilation aborted.  
    no  
    configure: error: could not determine flags for linking embedded Perl.  
    This probably means that ExtUtils::Embed or ExtUtils::MakeMaker is not  
    installed.  
    解决方法:  
    yum install perl-ExtUtils-Embed -y  
    
    问题3:  
    configure: error: could not determine flags for linking embedded Perl.  
    This probably means that ExtUtils::Embed or ExtUtils::MakeMaker is not  
    installed.  
    解决方法:  
    yum install perl-ExtUtils-Embed  
      
    问题4:  
    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.  
      
    解决方法:  
    yum install readline readline-devel  
      
    问题5:  
    checking for inflate in -lz... no  
    configure: error: zlib library not found  
    If you have zlib already installed, see config.log for details on the  
    failure. It is possible the compiler isn't looking in the proper directory.  
    Use --without-zlib to disable zlib support.  
    解决方法:  
    yum install zlib zlib-devel  
      
      
    问题6:  
    checking for CRYPTO_new_ex_data in -lcrypto... no  
    configure: error: library 'crypto' is required for OpenSSL  
    解决方法:  
    yum install openssl openssl-devel  
      
    问题7:  
    checking for pam_start in -lpam... no  
    configure: error: library 'pam' is required for PAM  
    解决方法:  
    yum install pam pam-devel  
      
    问题8:  
    checking for xmlSaveToBuffer in -lxml2... no  
    configure: error: library 'xml2' (version >= 2.6.23) is required for XML support  
    解决方法:  
    yum install libxml2 libxml2-devel  
      
    问题9:  
    checking for xsltCleanupGlobals in -lxslt... no  
    configure: error: library 'xslt' is required for XSLT support  
    解决方法:  
    yum install libxslt libxslt-devel  
      
      
    问题10:  
    configure: error: Tcl shell not found  
    解决方法:  
    yum install tcl tcl-devel  
      
      
    问题11:  
    checking for ldap.h... no  
    configure: error: header file is required for LDAP  
    解决方法:  
    yum install openldap openldap-devel  
      
    问题12:  
    checking for Python.h... no  
    configure: error: header file <Python.h> is required for Python  
    解决方法:  
    yum install python python-devel 
    

    以上这些依赖库都安装成功后,再运行上面的configure命令,就能成功啦。

    接下来就是编译安装啦:

    make && make install
    

    这两个命令可能会比较慢,执行完成后,会提示成功。

    然后你就会发现在/opt/目录下找到你安装的psql了

    其中:

    /opt/psql/bin里面放的是可执行命令,比如createdb之类的;

    /opt/psql/lib里面放的是库文件;

    /opt/psql/include里面放的是头文件;

    /opt/psql/share是相关的资源文件。

    这些文件如果在configure命令中没有指定--prefix的话,会安装到/usr/local/目录下,以后要删除的时候就要一个个找了,比较麻烦。

    好的 我们安装好了postgresql程序,接下来进行配置。

    为了安全考虑,postgresql不允许使用root用户操作数据库,我们在系统中为使用postgresql添加一个用户postgres:

    adduser postgres
    

    创建密码:

    passwd postgres
    

    然后切换到postgres用户下面:

    su - postgres
    

    编辑/home/postgres下的.bash_profile

    设置以下的环境变量

    export PGHOME=/opt/psql  (这个就是我们的安装目录)
    
    export PGDATA=~/data        (数据存放的目录,这个看你高兴了,不要求一定放在这里)
    
    export PATH=$PATH:$HOME/bin:$PGHOME/bin

    然后source一下

    source  ~/.bash_profile
    

    使环境变量生效。

    接下来初始化数据库,使用initdb命令(如果提示command not found,那么很有可能是你上面的PGHOME设置错误或者没有source一下)

    #初始化数据库
    initdb 
    

    数据库的初始化完成后运行:

    pg_ctl start
    

    启动postgres数据库实例。此时你就可以使用:

    ps -ef | grep postgres
    

    现在我们可以进入数据库,使用如下命令:

    psql -h 127.0.0.1 -d postgres -U postgres

      

    如果我们比较懒,不想每次登录手动启动psql,那么设置下psql开机启动。

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

      

    linux文件即为自启动脚本。

    1)修改linux文件属性,添加X属性(这里如果提示无权限的话,切换到root用户进行操作)

    chmod a+x linux
    

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

    cp linux /etc/init.d/postgresql
    

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

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

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

    执行service postgresql start,就可以启动PostgreSQL服务

    4) 执行service postgresql start,就可以启动PostgreSQL服务

    service postgresql start
    

    5)设置postgresql服务开机自启动

    chkconfig --add postgresql
    

    执行上面的命令,就可以实现postgresql服务的开机自启动。

    接下来就是PostgreSQL的远程连接设置

    1) 设置远程访问认证机制

    编辑/home/postgres/PGHOME/pg_dat/pg_hba.conf 文件:

    说明:

    每一行有五个字段,

    # TYPE DATABASE USER CIDR-ADDRESS METHOD

    分别是:连接类型、可使用的数据库名、使用者、DIDR地址、和验证方法等五项。

    在该配置文件中的加上图中最后两行 IPv4 remote connections的配置,把该网段的IP配置进去,就可以。

    2. 改监听地址

    默认下,POSTGRESQL只接受本地服务,要接受远程服务,需改postgresql.conf 文件listen_address = *

    配置完后,重新启动一下数据库,既可以进行远程访问数据库了。

    以上是我安装配置PostgreSQL的大概过程,后续遇到的问题会在文章中持续更新,有错误欢迎提出指正,谢谢~

  • 相关阅读:
    详解应对平台高并发的分布式调度框架TBSchedule
    利用httpd配置虚拟目录创建下载站点
    在centos7下用http搭建配置svn服务
    centos7/rhel7下配置PXE+Kickstart自动安装linux系统
    centos7破解安装confluence5.9.11
    centos7破解安装jira6.3.6(含Agile)
    mysql5.6做单向主从复制Replication
    解决忘记mysql中的root用户密码问题
    centos7安装mysql5.6(rpm包安装)
    centos7/RHEL7最小化系统安装gnome图形界面
  • 原文地址:https://www.cnblogs.com/dengyulinBlog/p/6424462.html
Copyright © 2011-2022 走看看