添加RPM
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
安装PostgreSQL 9.6
postgresql96-server 数据库核心服务端
postgresql96-contrib 附加第三方扩展
postgresql96-devel C语言开发Header头文件和库(可以不装)
yum install postgresql96-server postgresql96-contrib postgresql96-devel
验证是否安装成功
rpm -aq| grep postgres
更改数据库数据存储路径(原默认路径为:/var/lib/pgsql/9.6/data )
cd /mnt
sudo mkdir vdb1
sudo chown -R postgres:postgres vdb1
sudo chmod 700 vdb1
vi /usr/lib/systemd/system/postgresql-9.6.service
初始化数据库
/usr/pgsql-9.6/bin/postgresql96-setup initdb
开启服务
service postgresql-9.6start 或者 systemctl start postgresql-9.6.service
开机启动
sudo chkconfig postgresql-9.6 on 或者 systemctl enable postgresql-9.6.service
修改密码
su postgres
psql
ALTER USER postgres WITH PASSWORD '密码'; --必须以分号结束,成功执行后会出现ALTER ROLE
\q
su root
开启远程访问
vi /var/lib/pgsql/9.6/data/postgresql.conf 或者 vi /mnt/vdb1/postgresql.conf
修改#listen_addresses = 'localhost' 为 listen_addresses='*'
当然,此处‘*’也可以改为任何你想开放的服务器IP
信任远程连接
vi /var/lib/pgsql/9.6/data/pg_hba.conf 或者 vi /mnt/vdb1/pg_hba.conf
修改如下内容,信任指定服务器连接
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 10.211.55.6/32(需要连接的服务器IP,或者直接改为0.0.0.0/0,表示任意服务器连接) md5
重启服务
service postgresql-9.6 restart 或者 systemctl restart postgresql-9.6.service
配置防火墙
CentOS 防火墙中内置了PostgreSQL服务,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,我们只需以服务方式将PostgreSQL服务开放即可。
systemctl enable firewalld 开机启用防火墙
systemctl start firewalld 开启防火墙
firewall-cmd --add-service=postgresql --permanent 开放postgresql服务
firewall-cmd --zone=public --add-port=5432/tcp --permanent 或者可以直接添加端口
firewall-cmd --reload 重载防火墙
firewall-cmd --list-ports 查看占用端口
简单使用
psql -U postgres postgres 连接数据库
说明:-h表示主机(Host),-p表示端口(Port),-U表示用户(User)
显示所有数据库: \l
卸载PostgreSQL
yum erase postgresql96
结束
备注:
[root@ip-10-0-0-228 ec2-user]# service postgresql initdbData directory is not empty! [FAILED]
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]# ll -lhtr /var/lib/pgsql9/data/
total 88K
-rw——- 1 postgres postgres 4 May 5 09:32 PG_VERSION
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_twophase
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_tblspc
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_stat_tmp
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_snapshots
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_serial
drwx—— 4 postgres postgres 4.0K May 5 09:32 pg_multixact
-rw——- 1 postgres postgres 20K May 5 09:32 postgresql.conf
-rw——- 1 postgres postgres 1.6K May 5 09:32 pg_ident.conf
-rw——- 1 postgres postgres 4.2K May 5 09:32 pg_hba.conf
drwx—— 3 postgres postgres 4.0K May 5 09:32 pg_xlog
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_subtrans
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_clog
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_notify
drwx—— 2 postgres postgres 4.0K May 5 09:32 global
drwx—— 5 postgres postgres 4.0K May 5 09:32 base
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_log
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]# rm -rf /var/lib/pgsql9/data/*
[root@ip-10-0-0-228 ec2-user]# ll -lhtr /var/lib/pgsql9/data/
total 0
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]# service postgresql initdb
Initializing database: [ OK ]
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]# ll -lhtr /var/lib/pgsql9/data/
total 88K
-rw——- 1 postgres postgres 4 May 7 10:42 PG_VERSION
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_twophase
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_tblspc
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_stat_tmp
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_snapshots
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_serial
drwx—— 4 postgres postgres 4.0K May 7 10:42 pg_multixact
-rw——- 1 postgres postgres 20K May 7 10:42 postgresql.conf
-rw——- 1 postgres postgres 1.6K May 7 10:42 pg_ident.conf
-rw——- 1 postgres postgres 4.2K May 7 10:42 pg_hba.conf
drwx—— 3 postgres postgres 4.0K May 7 10:42 pg_xlog
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_subtrans
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_clog
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_notify
drwx—— 2 postgres postgres 4.0K May 7 10:42 global
drwx—— 5 postgres postgres 4.0K May 7 10:42 base
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_log
[root@ip-10-0-0-228 ec2-user]#
转载于 Qbit编程学习笔记