202、如何安装postgresql
前言:
PostgreSQL作为目前DB-Engines排行榜的第4名,越来越受到很多企业的青睐,故学习PG从安装开始,本文章就是描述如何搭建一个单实例的postgresql数据库系统。
安装步骤:
1.创建系统层面用户
groupadd postgresgrp
useradd -g postgresgrp -G wheel postgres
passwd postgres
2.相应目录
/data/johnny/pgdata --数据目录
/apps/ --pg程序的存放目录
3.yum源安装
登录到pg的官方页面https://www.postgresql.org/download/linux/redhat/
根据自己的需求选择如下3项,之后会自动生成Install命令

执行如下命令安装PG的repository RPM,执行结束后会在/etc/yum.repos.d/目录下看到pgdg-redhat-all.repo
sudo yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
4.安装PostgreSQL
安装client端
sudo yum -y install postgresql12
安装server端,这是数据库服务器启动的核心程序
sudo yum -y install postgresql12-server
可以通过如下命令查看到目前有效的安装包
yum list | grep postgresql12
添加环境变量~/.bash_profile
export PGPORT=1921
export PGDATA=/data/johnny/pgdata
export LANG=en_US.utf8
export PGHOME=/usr/pgsql-12
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PG_HOME/bin
使生效
source ~/.bash_profile
添加软链接:
sudo ln -s /usr/pgsql-12/ /apps/pgsql
sudo chown -R postgres:postgresgrp /apps/pgsql
5.主要目录
$ tree -L 1 /apps/pgsql/
/apps/pgsql/
├── bin //PG程序都放在这个目录下
├── lib //PG的一些C,C++头文件
└── share //PG的文档,示例文件,一些扩展等
6.初始化数据库
/apps/pgsql/bin/initdb -D /data/johnny/pgdata/12/data -W -U root
注:
-D是指定数据目录,-W是在初始化过程中设置超级管理员的密码,-U是设置超级用户的名字,默认是postgres,我这里设置成了root
运行成功后将看到如下信息
fixing permissions on existing directory /data/johnny/pgdata/12/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 ... Asia/Shanghai
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:
/apps/pgsql/bin/pg_ctl -D /data/johnny/pgdata/12/data -l logfile start
7.启动数据库实例
pg_ctl
是PostgreSQL中重要的管理工具,可以实现启动,停止,重载数据库,查看数据库服务状态等功能,十分强大
启动
/apps/pgsql/bin/pg_ctl -D /data/johnny/pgdata/12/data/ start
停止
/apps/pgsql/bin/pg_ctl -D /data/johnny/pgdata/12/data/ stop
查看运行状态:
$/apps/pgsql/bin/pg_ctl -D /data/johnny/pgdata/12/data/ status
结果如下表示成功:
pg_ctl: server is running (PID: 20364)
/usr/pgsql-12/bin/postgres "-D" "/data/johnny/pgdata/12/data"
8.连接数据库实例
$ psql -h 127.0.0.1 -U root -d postgres
psql (12.2)
Type "help" for help.
postgres=#
10、基本命令:
postgres=# \help --可以查看所有可执行的命令
postgres=# \l --查看当前所有库信息
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+-------------+-------------+---------------------
postgres | =root | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | =root | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/"=root" + "=root"=CTc/"=root"
template1 | =root | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/"=root" + "=root"=CTc/"=root"
(3 rows)
postgres=# \du --查看当前用户信息
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
root | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
11.如果想远程连接数据库实例
则需要修改配置文件postgresql.conf
和pg_hba.conf
这两个配置文件均在数据文件路径下(启动时 -D参数指定的路径 )
$vim postgresql.conf
listen_addresses = '*' --修改监听定制为*,表示当前服务器的所有IP地址都可以监听
port = 5432 --当然也可以修改默认端口
max_connections = 100 --最大连接数
添加访问控制记录到配置文件pg_hba.conf的末尾,--将此行加入配置文件中
$vim pg_hba.conf
host all all 0.0.0.0/0 md5
注:
修改完postgresql.conf后需要restart数据库实例
pg_ctl -D /data/johnny/pgdata/12/data/ restart
如果只修改了pg_hba.conf,则仅reload即可
pg_ctl -D /data/johnny/pgdata/12/data/ reload