部署PostgreSQL服务器(入职小灰)

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37960324/article/details/82427698

部署PostgreSQL服务器

1、环境

centos7部署PostgreSQL9.6

2、安装

PostgreSQL9.6还没有在默认的yum库中,需要手动安装。

rpm -Uvh https://yum.postgresql.org/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

注:如果系统不支持证书会出现以下报错

[root@master opt]# rpm -ivh https://yum.postgresql.org/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm--insecure
获取https://yum.postgresql.org/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm--insecure
curl: (60) Peer's Certificate issuer is not recognized.
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.
错误:跳过 https://yum.postgresql.org/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm--insecure - 传输失败

解决方法:在本地服务器上传输rpm包

gengtiandeMacBook-Air:~ geng.tian$ scp -rp /Users/geng.tian/Downloads/pgdg-centos96-9.6-3.noarch.rpm [email protected]:/opt/

在服务器上直接rpm安装即可

[root@master opt]# rpm -Uvh pgdg-centos96-9.6-3.noarch.rpm 
警告:pgdg-centos96-9.6-3.noarch.rpm: 头V4 DSA/SHA1 Signature, 密钥 ID 442df0f8: NOKEY
准备中... ################################# [100%]
正在升级/安装...
   1:pgdg-centos96-9.6-3 ################################# [100%]
  yum install -y postgresql96 postgresql96-server postgresql96-contrib 

检查是否安装完成

[root@master opt]# rpm -aq| grep postgres
postgresql96-libs-9.6.10-1PGDG.rhel7.x86_64
postgresql96-server-9.6.10-1PGDG.rhel7.x86_64
postgresql96-9.6.10-1PGDG.rhel7.x86_64
postgresql96-contrib-9.6.10-1PGDG.rhel7.x86_64

3、配置

初始化数据库,默认路径为/var/lib/pgsql/9.6/data,也可以初始化到指定的文件夹,但文件夹必须是postgres拥有。

切换postgres用户

 
[root@master data]# su - postgres
su: failed to execute /bin/bash: 权限不够

解决方法:给/usr/bin/分配普通用户的权限

注:完成安装后将权限收回

[root@master data]# chown -R postgres:postgres postgresql
[root@master data]# chmod 700 postgresql
[root@master ~]# chmod 755 /usr/bin/
[root@master ~]# su - postgres 
上一次登录:三 9月 5 15:21:56 CST 2018pts/0 上
-bash-4.2$ 

初始化数据库

-bash-4.2$ /usr/pgsql-9.6/bin/initdb -D /data/postgresql
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.

creating directory /data/postgresql ... 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:

    /usr/pgsql-9.6/bin/pg_ctl -D /data/postgresql -l logfile start

-bash-4.2$ echo $?
0

配置连接权限。

注意PostgreSQL是按顺序来检查的,要插入到合适的位置,以防靠前的配置覆盖了靠后的配置。

vim /data/postgresql/pg_hba.conf

根据需要添加如下行。

host <database> <user> 0.0.0.0/0 md5

配置:

# local DATABASE USER METHOD [OPTIONS]

# host DATABASE USER ADDRESS METHOD [OPTIONS]
# hostssl DATABASE USER ADDRESS METHOD [OPTIONS]
# hostnossl DATABASE USER ADDRESS METHOD [OPTIONS]
  host mariadb mysql 0.0.0.0/0 md5
# (The uppercase items must be replaced by actual values.)
 

配置数据库参数

vim /data/postgresql/postgresql.conf

修改如下

listen_addresses = '*' 
max_connections = 100

shared_buffers = 128MB
dynamic_shared_memory_type = posix
shared_preload_libraries = 'pg_stat_statements'

log_destination = 'csvlog'
logging_collector = on######注:这里开启日志重定向
log_directory = '/var/log/postgresql'######注:这里指定日志存放路径
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
log_rotation_age = 1d
log_rotation_size = 0
log_line_prefix = '< %m > '
log_timezone = 'Asia/Shanghai'

datestyle = 'iso, mdy'
timezone = 'Asia/Shanghai'
lc_messages = 'en_US.UTF-8'
lc_monetary = 'en_US.UTF-8'
lc_numeric = 'en_US.UTF-8'
lc_time = 'en_US.UTF-8'
default_text_search_config = 'pg_catalog.english'

4、启动

systemctl enable postgresql-9.6s
systemctl start postgresql-9.6

注:如果修改存储路径或其他原因需要初始化时,启动命令用

systemctl restart postgresql-9.6

不然有可能导致在敲到"systemctl start po"用tab命令,可能会补全至:systemctl start poweroff.target 

关闭系统,不要问我为什么会补全poweroff的命令

5、连接

以postgres用户本地登录。这是默认的登录规则。

$ su - postgres
$ psql

猜你喜欢

转载自blog.csdn.net/qq_37960324/article/details/82427698
今日推荐