添加源
yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
下载postgreSQL12
yum -y install postgresql12-server
验证安装成功
psql --version
初始化DB
/usr/pgsql-12/bin/postgresql-12-setup initdb
启动并设置开机启动DB
systemctl enable postgresql-12.service
systemctl start postgresql-12.service
创建用户
su - postgres
createuser -a -d username -P
创建数据库
createdb -O username dbnamme
更改用户认证方式
打开pg_hba.conf
/var/lib/pgsql/12/data/pg_hba.conf
默认是peer认证,这里改成md5
设置外部连接
默认是只允许localhost本地连接,所以要向pg_hba.conf添加以下行。
host all all 0.0.0.0/0 md5
接下来打开postgresql.conf,有个listen_addresses的设置,默认虽然被注释掉了,但自己不设置的话还是会只监听本地地址。我这里是像下面这样,添加一行listen_addresses='*'
最后重启db就可以了
systemctl restart postgresql-12.service
备份,导入数据
如果你有之前旧版本备份的数据,也可以顺便导入进来。
备份
pg_dump database名 > backup_file名
恢复备份
psql database名 < backup_file名