前提:操作系统是centos7.7,关闭selinux和防火墙
一、安装postgresql12二进制包,(yum方式,也可以下载rpm包)
1、postgresql官方网站,二进制包安装向导页面
https://www.postgresql.org/download/linux/redhat/
2、安装postgresql的yum源
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y
3、下载postgresql的二进制安装rpm包(可选安装方式),网址
https://yum.postgresql.org/12/redhat/rhel-7-x86_64/repoview/
wget -P /root/ https://yum.postgresql.org/12/redhat/rhel-7-x86_64/postgresql12-libs-12.3-1PGDG.rhel7.x86_64.rpm
wget -P /root/ https://yum.postgresql.org/12/redhat/rhel-7-x86_64/postgresql12-12.3-1PGDG.rhel7.x86_64.rpm
wget -P /root/ https://yum.postgresql.org/12/redhat/rhel-7-x86_64/postgresql12-server-12.3-1PGDG.rhel7.x86_64.rpm
wget -P /root/ http://mirrors.163.com/centos/7/updates/x86_64/Packages/libicu-50.2-4.el7_7.x86_64.rpm
4、安装postgresql客户端
yum install postgresql12 -y
5、安装postgresql服务器端
yum install postgresql12-server -y
或者RPM安装
rpm -ivh /root/*.rpm
6、查看安装的postgresql安装位置
find / -name "*postg*"
find / -name "*pgsql*"
7、初始化postgresql 数据库
/usr/pgsql-12/bin/postgresql-12-setup initdb
8、设置postgresql自启动并且启动数据库
设置自启动
systemctl enable postgresql-12
启动数据库
systemctl restart postgresql-12
二、配置
1、配置postgresql监听支持远程访问的地址0.0.0.0;修改文件"/var/lib/pgsql/12/data/postgresql.conf"
vim /var/lib/pgsql/12/data/postgresql.conf
listen_addresses = 'localhost' 修改成 listen_addresses = '0.0.0.0'
三、测试
1、通过postgres用户,增加root用户和数据库
su - postgres
psql
2、增加超级管理员root用户
create role root superuser encrypted password '123456' login replication createdb createrole;
3、增加root数据库,并设置root数据库属于root用户
create database root owner root;
4、修改"/var/lib/pgsql/12/data/pg_hba.conf"调整root用户的远程访问权限
vim /var/lib/pgsql/12/data/pg_hba.conf
host all root 192.168.1.0/24 md5
5、修改"/var/lib/pgsql/12/data/pg_hba.conf"调整sysdb用户的远程访问权限,设置sysdb用户只能访问数据库sysdb
vim /var/lib/pgsql/12/data/pg_hba.conf
host sysdb sysdb 192.168.1.0/24 md5
数据库 用户
6、使用超级管理员root,通过远程登录增加用户sysdb
psql -h 192.168.1.181 -Uroot -d root
create user sysdb encrypted password '123456' login;
7、增加数据库sysdb,把数据库所属给sysdb用户
create database sysdb owner sysdb;
8、在sysdb库中增加表
create table abcd( id int primary key not null, a1 char(20) not null);
9、往sysdb库中的表abcd中写入1条数据
insert into abcd (id,a1)values(1,'abcd');
11、验证sysdb库中创建的表,
\d
12、验证sysdb库abcd表的数据
seletct * from abcd;
13、查看当前的用户,数据库
\conninfo
\c