1、PostgreSQL简介
PostgreSQL是一个基于POSTGRES 4.2的对象关系数据库管理系统。PostgreSQL项目为最常见的发行版提供了所有受支持版本的软件包的存储库。支持的发行版包括所有Red Hat系列,其中包括CentOS,Fedora,Scientific Linux,Oracle Linux和Red Hat Enterprise Linux。
2、安装PostgreSQL Yum源
[root@postgresql ~]# yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
[root@postgresql ~]# vim /etc/yum.repos.d/pgdg-redhat-all.repo
获取有关已安装软件包的更多信息:
[root@postgresql ~]# rpm -qi pgdg-redhat-repo
3、安装PostgreSQL客户端和服务器软件包
[root@postgresql ~]# yum -y install epel-release yum-utils
[root@postgresql ~]# yum-config-manager --enable pgdg12
[root@postgresql ~]# yum install postgresql12-server postgresql12 -y
4、初始化并启动数据库服务
(1)安装后,先进行postgresql数据库初始化
[root@postgresql ~]# /usr/pgsql-12/bin/postgresql-12-setup initdb
(2)postgresql数据库主配置文件
[root@postgresql ~]# vim /var/lib/pgsql/12/data/postgresql.conf
(3)启动、并设置开机自启数据库
[root@postgresql ~]# systemctl enable --now postgresql-12
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-12.service to /usr/lib/systemd/system/postgresql-12.service.
(4)查看postgresql数据库服务运行情况
[root@postgresql ~]# systemctl status postgresql-12
5、启用远程访问PostgreSQL
(1)编辑文件/var/lib/pgsql/12/data/postgresql.conf并将所有服务器的“监听地址”设置为服务器IP地址或“ *”
[root@postgresql ~]# vim /var/lib/pgsql/12/data/postgresql.conf
listen_addresses = '*'
port = 5432
(2)设置远程连接PostgreSQL接受远程连接
[root@postgresql ~]# vim /var/lib/pgsql/12/data/pg_hba.conf
# Accept from anywhere
host all all 0.0.0.0/0 md5
# Accept from trusted subnet
host all all 10.27.0.0/24 md5
[root@postgresql ~]# systemctl restart postgresql-12 #重启数据库
6、设置PostgreSQL管理员用户密码
[root@postgresql ~]# sudo su - postgres
-bash-4.2$ psql -c "alter user postgres with password 'ucloud.cn'"
7、创建测试用户和数据库
-bash-4.2$ createuser stargao_user
-bash-4.2$ createdb stargao_db -O stargao_user
-bash-4.2$ psql
postgres=# grant all privileges on database stargao_db to stargao_user;
GRANT
postgres=# ALTER USER stargao_user WITH PASSWORD 'ucloud.cn'; #添加用户密码
ALTER ROLE
[root@postgresql ~]# psql -Uroot -h10.27.0.224 -l #查看有哪些库可以连接
[root@postgresql ~]# psql -Ustargao_user -h10.27.0.224 -dstargao_db #连接PGSQL时需要指定库
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。