【实操篇】_记录PostgreSQL一次安装配置

													    Note-taker:Ethan_Yang		
													    Recording time: 2019/09/23
													    Location:BJ T3						
													    number of docs:12

【前言】
https://db-engines.com/en/ranking 看一组DB-ENGINE的排名;
既然PG排名辣么靠前(目前第四),加之工作需要,所以怎么着也要了解下,故有了以下这篇文章。
【实操篇】_记录PostgreSQL一次安装配置_第1张图片

以下是PG的官方介绍,权威且简单,就不翻译了。
https://www.postgresql.org/

What is PostgreSQL?
PostgreSQL is a powerful, open source object-relational database system that uses and extends the SQL language combined with many features that safely store and scale the most complicated data workloads. The origins of PostgreSQL date back to 1986 as part of the POSTGRES project at the University of California at Berkeley and has more than 30 years of active development on the core platform.
PostgreSQL has earned a strong reputation for its proven architecture, reliability, data integrity, robust feature set, extensibility, and the dedication of the open source community behind the software to consistently deliver performant and innovative solutions. PostgreSQL runs on all major operating systems, has been ACID-compliant since 2001, and has powerful add-ons such as the popular PostGIS geospatial database extender. It is no surprise that PostgreSQL has become the open source relational database of choice for many people and organisations.
Getting started with using PostgreSQL has never been easier - pick a project you want to build, and let PostgreSQL safely and robustly store your data.

PostgreSQL,简称PG,是一个功能强大的开源对象关系性数据库管理系统,给自己的标签为“世界上最先进的开源数据库”( The world’s most advanced open source database)。可以理解,宣传自己之前,怎么也要给自己拉一杆大旗,正如MySQL给自己立的flag“世界上最受欢迎的开源数据库”一样。

PostgreSQL具有极高的可扩展性。可添加自己定义的数据类型,开发自定义函数,甚至可以编写各种编程语言的代码,无需重新编译库。

本文将演示如何在RHEL 7 中安装PostgreSQL数据库。

OS系统信息:
[root@ethanyl 11]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.2 (Maipo)

首先从PG官网拉取最新版本11的rpm安装包。
https://www.postgresql.org/download/linux/redhat/
【实操篇】_记录PostgreSQL一次安装配置_第2张图片
查看RHEL 7已经安装了PG9.2.13版本
[root@ethanyl ~]# rpm -qa | grep postgresqlpostgresql-server-9.2.13-1.el7_1.x86_64postgresql-libs-9.2.13-1.el7_1.x86_64postgresql-docs-9.2.13-1.el7_1.x86_64postgresql-9.2.13-1.el7_1.x86_64

如下步骤按照官网操作步骤如下:

安装PostgreSQL

  1. Install the repository RPM
    使用yum方式安装PG11pgdg-redhat-repo-latest.noarch.rpm

    [root@ethanyl mysqlsoft]# yum install \ https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-managerThis system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.pgdg-redhat-repo-latest.noarch.rpm | 5.6 kB 00:00:00 Examining /var/tmp/yum-root-Z5mZ3Y/pgdg-redhat-repo-latest.noarch.rpm: pgdg-redhat-repo-42.0-4.noarchMarking /var/tmp/yum-root-Z5mZ3Y/pgdg-redhat-repo-latest.noarch.rpm to be installedResolving Dependencies–> Running transaction check—> Package pgdg-redhat-repo.noarch 0:42.0-4 will be installed–> Finished Dependency ResolutioDependencies Resolved===================================================================================== Package Arch Version Repository Size========Installing: pgdg-redhat-repo noarch 42.0-4 /pgdg-redhat-repo-latest.noarch 6.8 kTransaction Summary=Install 1 Package Total size: 6.8 kInstalled size: 6.8 kIs this ok [y/d/N]: yDownloading packages:Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing :pgdg-redhat-repo-42.0-4.noarch 1/1 Verifying :pgdg-redhat-repo-42.0-4.noarch 1/1 Installed: pgdg-redhat-repo.noarch 0:42.0-4 Complete!

    1. Install the client packages:
      [root@ethanyl mysqlsoft]# yum install postgresql11Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-managerThis system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.pgdg10 | 3.6 kB 00:00:00 pgdg11 | 3.6 kB 00:00:00 pgdg94 | 3.6 kB 00:00:00 pgdg95 | 3.6 kB 00:00:00 pgdg96 | 3.6 kB 00:00:00 (1/10): pgdg10/7Server/x86_64/group_gz | 245 B 00:00:01 (2/10): pgdg94/7Server/x86_64/group_gz | 247 B 00:00:01 (3/10): pgdg95/7Server/x86_64/group_gz | 249 B 00:00:00 (4/10): pgdg94/7Server/x86_64/primary_db | 307 kB 00:00:01 (5/10): pgdg95/7Server/x86_64/primary_db | 302 kB 00:00:01 (6/10): pgdg10/7Server/x86_64/primary_db | 286 kB 00:00:02 (7/10): pgdg96/7Server/x86_64/group_gz | 249 B 00:00:00 (8/10): pgdg96/7Server/x86_64/primary_db | 303 kB 00:00:00 (9/10): pgdg11/7Server/x86_64/primary_db | 231 kB 00:00:03 (10/10): pgdg11/7Server/x86_64/group_gz | 245 B 00:00:16 Resolving Dependencies–> Running transaction check—> Package postgresql11.x86_64 0:11.4-1PGDG.rhel7 will be installed–> Processing Dependency: postgresql11-libs(x86-64) = 11.4-1PGDG.rhel7 for package: postgresql11-11.4-1PGDG.rhel7.x86_64–> Running transaction check—> Package postgresql11-libs.x86_64 0:11.4-1PGDG.rhel7 will be installed–> Finished Dependency Resolution Dependencies Resolved ============================================================================================== Package Arch Version Repository Size=Installing: postgresql11 x86_64 11.4-1PGDG.rhel7 pgdg11 1.6 MInstalling for dependencies: postgresql11-libs x86_64 11.4-1PGDG.rhel7 pgdg11 361 k Transaction Summary=Install 1 Package (+1 Dependent package) Total download size: 2.0 MInstalled size: 10 MIs this ok [y/d/N]: yDownloading packages⚠️ /var/cache/yum/x86_64/7Server/pgdg11/packages/postgresql11-libs-11.4-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEYPublic key for postgresql11-libs-11.4-1PGDG.rhel7.x86_64.rpm is not installed(1/2): postgresql11-libs-11.4-1PGDG.rhel7.x86_64.rpm | 361 kB 00:00:03 (2/2): postgresql11-11.4-1PGDG.rhel7.x86_64.rpm | 1.6 MB 00:00:04 ---------------------------------------------------------------------------------------------------------------------------------------Total 444 kB/s | 2.0 MB 00:00:04 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDGImporting GPG key 0x442DF0F8: Userid : “PostgreSQL RPM Building Project [email protected]” Fingerprint: 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8 Package : pgdg-redhat-repo-42.0-4.noarch (@/pgdg-redhat-repo-latest.noarch) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDGIs this ok [y/N]: yRunning transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : postgresql11-libs-11.4-1PGDG.rhel7.x86_64 1/2 Installing : postgresql11-11.4-1PGDG.rhel7.x86_64 2/2 Verifying : postgresql11-11.4-1PGDG.rhel7.x86_64 1/2 Verifying : postgresql11-libs-11.4-1PGDG.rhel7.x86_64 2/2 Installed: postgresql11.x86_64 0:11.4-1PGDG.rhel7 Dependency Installed: postgresql11-libs.x86_64 0:11.4-1PGDG.rhel7 Complete!
    2. Optionally install the server packages:
      [root@ethanyl mysqlsoft]# yum install postgresql11-server Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-managerThis system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.Resolving Dependencies–> Running transaction check—> Package postgresql11-server.x86_64 0:11.4-1PGDG.rhel7 will be installed–> Finished Dependency Resolution Dependencies Resolved =============================================================================================== Package Arch Version Repository Size====Installing: postgresql11-server x86_64 11.4-1PGDG.rhel7 pgdg11 4.7 M Transaction Summary=Install 1 Package Total download size: 4.7 MInstalled size: 19 MIs this ok [y/d/N]: yDownloading packages:postgresql11-server-11.4-1PGDG.rhel7.x86_64.rpm | 4.7 MB 00:00:06 Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : postgresql11-server-11.4-1PGDG.rhel7.x86_64 1/1 Verifying : postgresql11-server-11.4-1PGDG.rhel7.x86_64 1/1 Installed: postgresql11-server.x86_64 0:11.4-1PGDG.rhel7 Complete!

[root@ethanyl postgresql]# yum install postgresql11-contrib

Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-managerThis system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.Resolving Dependencies–> Running transaction check—> Package postgresql11-contrib.x86_64 0:11.4-1PGDG.rhel7 will be installed–> Finished Dependency Resolution Dependencies Resolved ==================================================================================================== Package Arch Version Repository SizeInstalling: postgresql11-contrib x86_64 11.4-1PGDG.rhel7 pgdg11 617 k Transaction Summary=======Install 1 Package Total download size: 617 kInstalled size: 2.1 MIs this ok [y/d/N]: yDownloading packages:postgresql11-contrib-11.4-1PGDG.rhel7.x86_64.rpm | 617 kB 00:00:06 Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : postgresql11-contrib-11.4-1PGDG.rhel7.x86_64 1/1 Verifying : postgresql11-contrib-11.4-1PGDG.rhel7.x86_64 1/1 Installed: postgresql11-contrib.x86_64 0:11.4-1PGDG.rhel7 Complete!
4. Optionally initialize the database and enable automatic start:
[root@ethanyl postgresql]# /usr/pgsql-11/bin/postgresql-11-setup initdbInitializing database … OK
[root@ethanyl postgresql]# systemctl start postgresql-11.service [root@ethanyl postgresql]# systemctl enable postgresql-11.service Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-11.service to /usr/lib/systemd/system/postgresql-11.service.[root@ethanyl postgresql]# systemctl status postgresql-11.service * postgresql-11.service - PostgreSQL 11 database server Loaded: loaded (/usr/lib/systemd/system/postgresql-11.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2019-07-22 11:52:58 CST; 1min 11s ago Docs: https://www.postgresql.org/docs/11/static/ Main PID: 9221 (postmaster) CGroup: /system.slice/postgresql-11.service |-9221 /usr/pgsql-11/bin/postmaster -D /var/lib/pgsql/11/data/ |-9223 postgres: logger |-9225 postgres: checkpointer |-9226 postgres: background writer |-9227 postgres: walwriter |-9228 postgres: autovacuum launcher |-9229 postgres: stats collector `-9230 postgres: logical replication launcher Jul 22 11:52:58 ethanyl postmaster[9221]: 2019-07-22 11:52:58.821 CST [9221] LOG: listening on I…5432Jul 22 11:52:58 ethanyl postmaster[9221]: 2019-07-22 11:52:58.821 CST [9221] LOG: listening on I…5432Jul 22 11:52:58 ethanyl postmaster[9221]: 2019-07-22 11:52:58.821 CST [9221] LOG: could not bind… useJul 22 11:52:58 ethanyl postmaster[9221]: 2019-07-22 11:52:58.821 CST [9221] HINT: Is another po…try.Jul 22 11:52:58 ethanyl postmaster[9221]: 2019-07-22 11:52:58.821 CST [9221] LOG: listening on I…5432Jul 22 11:52:58 ethanyl postmaster[9221]: 2019-07-22 11:52:58.824 CST [9221] LOG: listening on U…432"Jul 22 11:52:58 ethanyl postmaster[9221]: 2019-07-22 11:52:58.825 CST [9221] LOG: listening on U…432"Jul 22 11:52:58 ethanyl postmaster[9221]: 2019-07-22 11:52:58.840 CST [9221] LOG: redirecting lo…cessJul 22 11:52:58 ethanyl postmaster[9221]: 2019-07-22 11:52:58.840 CST [9221] HINT: Future log ou…og".Jul 22 11:52:58 ethanyl systemd[1]: Started PostgreSQL 11 database server.Hint: Some lines were ellipsized, use -l to show in full.
以上,基本的PG安装完毕。

二、 配置PostgreSQL数据库

  1. 系统用户postgres帐户创建密码

[root@ethanyl data]# passwd postgres

切换到postgres系统用户帐户,设置PostgreSQL管理数据库用户帐户。
这里讲下一个简单密码策略,也是Oracle密码策略的推荐:

密码长度不少于8位,且至少包含一个大写字母、一个数字、一个特殊字符。

设置PG库的用户postgres

[root@ethanyl data]#su - postgres-bash-4.2$ psql -c “ALTER USER postgres WITH PASSWORD ‘Ethan_135#’;”
PostgreSQL配置文件可以在目录中找到/var/lib/pgsql/11/data。
其中,主服务器配置文件是
/var/lib/pgsql/data/postgresql.conf;/var/lib/pgsql/data/pg_hba.conf配置客户端身份验证。
[root@ethanyl data]# pwd/var/lib/pgsql/11/data[root@ethanyl data]# lltotal 60-rw------- 1 postgres postgres 3 Jul 22 11:50 PG_VERSIONdrwx------ 5 postgres postgres 38 Jul 22 11:51 base-rw------- 1 postgres postgres 30 Jul 22 11:52 current_logfilesdrwx------ 2 postgres postgres 4096 Jul 22 11:51 globaldrwx------ 2 postgres postgres 31 Jul 22 11:52 logdrwx------ 2 postgres postgres 6 Jul 22 11:50 pg_commit_tsdrwx------ 2 postgres postgres 6 Jul 22 11:50 pg_dynshmem-rw------- 1 postgres postgres 4269 Jul 22 11:50 pg_hba.conf-rw------- 1 postgres postgres 1636 Jul 22 11:50 pg_ident.confdrwx------ 4 postgres postgres 65 Jul 22 11:57 pg_logicaldrwx------ 4 postgres postgres 34 Jul 22 11:50 pg_multixactdrwx------ 2 postgres postgres 17 Jul 22 11:52 pg_notifydrwx------ 2 postgres postgres 6 Jul 22 11:50 pg_replslotdrwx------ 2 postgres postgres 6 Jul 22 11:50 pg_serialdrwx------ 2 postgres postgres 6 Jul 22 11:50 pg_snapshotsdrwx------ 2 postgres postgres 6 Jul 22 11:50 pg_statdrwx------ 2 postgres postgres 24 Jul 22 12:03 pg_stat_tmpdrwx------ 2 postgres postgres 17 Jul 22 11:50 pg_subtransdrwx------ 2 postgres postgres 6 Jul 22 11:50 pg_tblspcdrwx------ 2 postgres postgres 6 Jul 22 11:50 pg_twophasedrwx------ 3 postgres postgres 58 Jul 22 11:50 pg_waldrwx------ 2 postgres postgres 17 Jul 22 11:50 pg_xact-rw------- 1 postgres postgres 88 Jul 22 11:50 postgresql.auto.conf-rw------- 1 postgres postgres 23893 Jul 22 11:50 postgresql.conf-rw------- 1 postgres postgres 58 Jul 22 11:52 postmaster.opts-rw------- 1 postgres postgres 103 Jul 22 11:52 postmaster.pid
2. 配置客户端身份验证

PostgreSQL数据库系统支持基于密码的身份验证。基于密码的身份验证方法:md5,crypt或password(以明文形式发送密码)。
为防止攻击者密码嗅探,避免以明文形式在服务器上存储密码,推荐使用使用md5。

vi /var/lib/pgsql/11/data/pg_hba.conf

将身份验证方法更改为md5。
【实操篇】_记录PostgreSQL一次安装配置_第3张图片

  1. 重新启动Postgres使配置中生效。
    [root@ethanyl data]# systemctl reload postgresql

4.切换到postgres帐户并开始使用PostgreSQL。
[-bash-4.2$ psqlpsql (9.2.13, server 11.4)WARNING: psql version 9.2, server version 11.0. Some psql features might not work.Type “help” for help.
postgres=# \d pg_stat_activity View “pg_catalog.pg_stat_activity” Column | Type | Modifiers ------------------±-------------------------±---------- datid | oid | datname | name | pid | integer | usesysid | oid | usename | name | application_name | text | client_addr | inet | client_hostname | text | client_port | integer | backend_start | timestamp with time zone | xact_start | timestamp with time zone | query_start | timestamp with time zone | state_change | timestamp with time zone | wait_event_type | text | wait_event | text | state | text | backend_xid | xid | backend_xmin | xid | query | text | backend_type | text |

至此,pg的安装和简单配置完成。

【总结】

  1. 本文将简单演示如何在RHEL 7 中安装PostgreSQL11版本步骤,这里并没有给出高可用架构的部署,所以本文只能当练手熟悉用;
  2. 对于PostgreSQL作者还是新手,后续有待继续学习。

【官方文档】
https://www.postgresql.org/download/linux/redhat/

如果大家觉得此文有帮助,欢迎关注个人微信公众号;
长按识别二维码或公众号搜索“一森咖记”
【实操篇】_记录PostgreSQL一次安装配置_第4张图片

你可能感兴趣的:(PostgreSQL)