Postgresql学习笔记之——数据库安装

在Linux系统上使用源码安装Postgresql数据库。
系统:Centos 6.5
数据库:Postgresql 12.1

一、下载源码包。
下载地址:Postgresql官网下载

二、将下载后的源码包拷贝至Linux服务器上,并解压。

[root@local Desktop]# ls
postgresql-12.1.tar.gz
[root@local Desktop]# tar -xvf postgresql-12.1.tar.gz

三、安装依赖包:
此次安装依赖包使用yum命令安装,至于如何手动配置本地yum源参考文章:Linux配置本地yum源

[root@local ~]# yum -y install gcc gcc-c++ zlib-devel readline-devel tcl-devel bison flex perl-ExtUtils-Embed pam-devel libxml2-devel libxslt-devel openldap-devel python-devel openssl-devel cmake 

PS:依赖包也可以在源码编译时根据提示进行安装。安装时zlib-devel与readline-devel可能会漏掉,可以单独使用rpm工具安装,安装时加 --nodeps 去掉依赖即可。

四、切换到解压后的目录,然后进行编译和安装。

1.编译:

[root@local Desktop]# mkdir /data/pg12.1    # 创建软件安装目录
[root@local Desktop]# cd postgresql-12.1
[root@local postgresql-12.1]# ./configure --prefix=/data/pg12.1 --with-pgport=1921   --with-perl --with-python --with-openssl

PS:编译简单,只是指定了安装目录与端口号。目录提前创建好,编译时不会验证目录是否存在,到正式安装时就必须要有目录了,编译时端口后的三个建议加上,后面操作有会涉及到功能,避免重复编译,在一开始就编译上。

注意:编译时有可能因为依赖包没有安装完全出现编译错误,下面简单列几项错误解决方法:

问题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 
   
yum -y install 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 -y 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 -y 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 -y 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 -y install zlib zlib-devel 
   
   
问题6: 
checking for CRYPTO_new_ex_data in -lcrypto... no 
configure: error: library 'crypto' is required for OpenSSL 
解决方法: 
yum -y install openssl openssl-devel 
   
问题7: 
checking for pam_start in -lpam... no 
configure: error: library 'pam' is required for PAM 
解决方法: 
yum -y 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 -y install libxml2 libxml2-devel 
   
问题9: 
checking for xsltCleanupGlobals in -lxslt... no 
configure: error: library 'xslt' is required for XSLT support 
解决方法: 
yum  -y install libxslt libxslt-devel 
   
   
问题10: 
configure: error: Tcl shell not found 
解决方法: 
yum -y install tcl tcl-devel 
   
   
问题11: 
checking for ldap.h... no 
configure: error: header file is required for LDAP 
解决方法: 
yum -y install openldap openldap-devel 
   
问题12: 
checking for Python.h... no 
configure: error: header file <Python.h> is required for Python 
解决方法: 
yum -y install python python-devel

问题13: 
checking for CRYPTO_new_ex_data in -lcrypto... no
configure: error: library 'crypto' is required for OpenSSL
解决方法: 
yum -y install openssl-devel 

2.安装第三方插件,因为很多会用到,所以先提前安装,gmake world是将解压包下contrid目录中所有第三方插件都安装的命令:

[root@local postgresql-12.1]# gmake world
......
PostgreSQL, contrib, and documentation successfully made. Ready to install.

3.插件安装完成后,正式安装PG:

[root@local postgresql-12.1]# gmake install-world
......
PostgreSQL, contrib, and documentation installation complete.

五、安装完毕后,在系统中创建一个普通用户,用于初始化数据库,以及关闭、启动数据库。

[root@local ~]# useradd postgres
pa[root@local ~]# passwd postgres
Changing password for user postgres.
New password: 
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@local ~]# su - postgres

六、将安装好的数据库链接到一个常用的目录下,目的是为了以后方便升级。

[root@local ~]# ln -s /data/pg12.1 /data/pgsql
[root@local ~]# cd /data/
[root@local data]# ls
pg12.1  pgsql

pgsql目录就是使用的安装目录

七、配置环境变量,初始化数据库:

1.配置文件信息如下:

[root@local data]# su - postgres
[postgres@local ~]$ vim .bash_profile 
[postgres@local ~]$ cat .bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

export PGPOST=1921
export PGDATA=/data/pgsql_data
export LANG=en_US.UTF-8
export PGHOME=/data/pgsql
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export DATE=`date +"%Y%m%d%H%M"`
export PATH=$PGHOME/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
export PGUSER=postgres
export PGHOST=$PGDATA
alias rm='rm -i'
alias ll='ls -lh'
export PGDATABASE=postgres

2.根据配置文件创建postgresql数据库使用的数据存放目录。

[root@local ~]# mkdir /data/pgsql_data

更改属主和属组,在此有点需要注意,我使用ln连接的目录也在/data目录下,不能直接 chown -R进行全部更改属主和属组,先更改/data目录,然后在更改/pgsql_data,保持原始安装目录pgsql12.1和链接形成的pgsql目录属性不变。

[root@local ~]# chown postgres:postgres /data
[root@local ~]# cd /data
[root@local data]# chown postgres:postgres pgsql_data
root@local data]# ll
total 8
drwxr-xr-x 6 root     root     4096 Feb 12 21:03 pg12.1
lrwxrwxrwx 1 root     root       12 Feb 12 21:10 pgsql -> /data/pg12.1
drwxr-xr-x 2 postgres postgres 4096 Feb 12 21:17 pgsql_data

3.初始化数据库。

切换postgres用户,可以使用psql命令查看数据库版本:

[root@local data]# su - postgres
[postgres@local ~]$ psql -V
psql (PostgreSQL) 12.1

开始初始化数据库,初始化开始会提示输入用户密码,此处用户指的是参数-U后面的postgres用户,postgres初始化后会成为超级用户:

[postgres@local ~]$ initdb -D $PGDATA -E UTF8 --locale=C -U postgres -W
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 "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

Enter new superuser password: 
Enter it again: 

fixing permissions on existing directory /data/pgsql_data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... PRC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: 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/pgsql_data -l logfile start

初始化完成后会提示启动数据库,先不启动数据库,启动前修改一下数据库的参数文件与系统的内核参数。
Postgresql数据库的主要参数文件有两个:pg_hba.conf、postgresql.conf,位置在$PGDATA下。

[postgres@local ~]$ cd $PGDATA
[postgres@local pgsql_data]$ ls
base          pg_dynshmem    pg_logical    pg_replslot   pg_stat      pg_tblspc    pg_wal                postgresql.conf
global        pg_hba.conf    pg_multixact  pg_serial     pg_stat_tmp  pg_twophase  pg_xact
pg_commit_ts  pg_ident.conf  pg_notify     pg_snapshots  pg_subtrans  PG_VERSION   postgresql.auto.conf

修改pg_hba.conf文件,最后一行添加host all all 0.0.0.0/0 md5 所有ip都可通过密码访问数据库:

[postgres@local pgsql_data]$ cat pg_hba.conf 
# PostgreSQL Client Authentication Configuration File
# ===================================================
#
# Refer to the "Client Authentication" section in the PostgreSQL
# documentation for a complete description of this file.  A short
# synopsis follows.
#
# This file controls: which hosts are allowed to connect, how clients
# are authenticated, which PostgreSQL user names they can use, which
# databases they can access.  Records take one of these forms:
#
# local      DATABASE  USER  METHOD  [OPTIONS]
# host       DATABASE  USER  ADDRESS  METHOD  [OPTIONS]
# hostssl    DATABASE  USER  ADDRESS  METHOD  [OPTIONS]
# hostnossl  DATABASE  USER  ADDRESS  METHOD  [OPTIONS]
#
# (The uppercase items must be replaced by actual values.)
#
# The first field is the connection type: "local" is a Unix-domain
# socket, "host" is either a plain or SSL-encrypted TCP/IP socket,
# "hostssl" is an SSL-encrypted TCP/IP socket, and "hostnossl" is a
# plain TCP/IP socket.
#
# DATABASE can be "all", "sameuser", "samerole", "replication", a
# database name, or a comma-separated list thereof. The "all"
# keyword does not match "replication". Access to replication
# must be enabled in a separate record (see example below).
#
# USER can be "all", a user name, a group name prefixed with "+", or a
# comma-separated list thereof.  In both the DATABASE and USER fields
# you can also write a file name prefixed with "@" to include names
# from a separate file.
#
# ADDRESS specifies the set of hosts the record matches.  It can be a
# host name, or it is made up of an IP address and a CIDR mask that is
# an integer (between 0 and 32 (IPv4) or 128 (IPv6) inclusive) that
# specifies the number of significant bits in the mask.  A host name
# that starts with a dot (.) matches a suffix of the actual host name.
# Alternatively, you can write an IP address and netmask in separate
# columns to specify the set of hosts.  Instead of a CIDR-address, you
# can write "samehost" to match any of the server's own IP addresses,
# or "samenet" to match any address in any subnet that the server is
# directly connected to.
#
# METHOD can be "trust", "reject", "md5", "password", "scram-sha-256",
# "gss", "sspi", "ident", "peer", "pam", "ldap", "radius" or "cert".
# Note that "password" sends passwords in clear text; "md5" or
# "scram-sha-256" are preferred since they send encrypted passwords.
#
# OPTIONS are a set of options for the authentication in the format
# NAME=VALUE.  The available options depend on the different
# authentication methods -- refer to the "Client Authentication"
# section in the documentation for a list of which options are
# available for which authentication methods.
#
# Database and user names containing spaces, commas, quotes and other
# special characters must be quoted.  Quoting one of the keywords
# "all", "sameuser", "samerole" or "replication" makes the name lose
# its special character, and just match a database or username with
# that name.
#
# This file is read on server startup and when the server receives a
# SIGHUP signal.  If you edit the file on a running system, you have to
# SIGHUP the server for the changes to take effect, run "pg_ctl reload",
# or execute "SELECT pg_reload_conf()".
#
# Put your actual configuration here
# ----------------------------------
#
# If you want to allow non-local connections, you need to add more
# "host" records.  In that case you will also need to make PostgreSQL
# listen on a non-local interface via the listen_addresses
# configuration parameter, or via the -i or -h command line switches.

# CAUTION: Configuring the system for local "trust" authentication
# allows any local user to connect as any PostgreSQL user, including
# the database superuser.  If you do not trust all your local users,
# use another authentication method.


# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust
host    all             all             0.0.0.0/0            md5

修改postgresql.conf参数文件:
(因为是测试环境,所以只修改简单几个参数即可)
listen_addresses = ‘*’
port = 1921
max_connections = 1000
unix_socket_directories = ‘.’ # 此参数打开后,本地登录不用指定 -h 127.0.0.1
unix_socket_permissions = 0700

修改Linux系统内核参数:

[root@local ~]# cat /etc/sysctl.conf
添加如下参数:
kernel.shmmni = 4096
kernel.sem = 50100 64128000 50100 1280
fs.file-max = 7672460
net.ipv4.ip_local_port_range = 9000 65000
net.core.rmem_default = 1048576
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
是参数生效:
[root@local ~]# sysctl -p
[root@local ~]# vim /etc/security/limits.conf
添加如下参数:
* soft nofile 131072
* hard nofile 131072
* soft nproc 131072
* hard nproc 131072
* soft core unlimited
* hard core unlimited
* soft memlock 50000000
* hard memlock 50000000

八、启动Postgresql数据库。

[postgres@local ~]$ pg_ctl start -D $PGDATA
waiting for server to start....2020-02-12 21:40:05.006 CST [18067] LOG:  starting PostgreSQL 12.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4), 64-bit
2020-02-12 21:40:05.006 CST [18067] LOG:  listening on IPv4 address "0.0.0.0", port 1921
2020-02-12 21:40:05.006 CST [18067] LOG:  listening on IPv6 address "::", port 1921
2020-02-12 21:40:05.016 CST [18067] LOG:  listening on Unix socket "/tmp/.s.PGSQL.1921"
2020-02-12 21:40:05.091 CST [18068] LOG:  database system was shut down at 2020-02-12 21:25:28 CST
2020-02-12 21:40:05.113 CST [18067] LOG:  database system is ready to accept connections
 done
server started

登录数据库,因为.bash_profile中环境变量配置了端口号、用户、数据目录等参数,所以直接psql就可以登录:

[postgres@local ~]$ psql 
psql (12.1)
Type "help" for help.

postgres=# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
(3 rows)

至此,Postgresql数据库源码安装完成。

扩展
Postgresql数据库的启动和关闭:
pg_ctl stop -D $PGDATA -m smart
pg_ctl stop -D $PGDATA -m fast
pg_ctl stop -D $PGDATA -m immediate
pg_ctl -D $PGDATA start

参数解析:

1.smart
最为安全,但最慢, 需要将所有连接都断开后,才会关库,默认关库模式。如果有连接没有断开会一直等待。

2.fast
强制中断会话,而不管有操作有没有提交,一般指定此参数来做数据库日常维护关闭命令

3.immediate
最暴力的方式,不管数据有没有写入磁盘,直接关掉数据库, 下次数据库启动时进行实例恢复, 如果在关库前有大量的事务没有写入磁盘, 那这个恢复过程就会花费较长时间。

配置开机自动启动

如果是使用yum命令安装,会自动配置服务脚本。如果是通过源码编译安装,则需要手动配置:

1.配置服务脚本
在解压后的源码包中有Linux、freebsd 、macos适用的服务脚本,如下

[root@local129 start-scripts]# pwd
/root/Desktop/postgresql-12.1/contrib/start-scripts
[root@local129 start-scripts]# ls
freebsd  linux  macos

将名称为Linux的脚本考本到/etc/init.d/目录中区,并命名为postgresql12,并赋予执行权限:

[root@local129 start-scripts]# cp linux /etc/init.d/postgresql12
[root@local129 start-scripts]# cd /etc/init.d/
[root@local129 init.d]# chmod +x postgresql12 
[root@local129 init.d]# ll -lh postgresql12 
-rwxr-xr-x 1 root root 3.5K Jun  8 18:42 postgresql12
[root@local129 init.d]# chkconfig postgresql12 on 

你可能感兴趣的:(Postgresql)