通过rpm查看是否已经安装postgreSQL
如果没有查询没有结果, 则说明没有安装
安装: 官方文档: https://www.postgresql.org/download/linux/redhat/
- 安装存储库RPM:
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
- 安装客户端软件包:
yum install postgresql96
- 安装服务器包:
yum install postgresql96-server
- 初始化数据库:
/usr/pgsql-9.6/bin/postgresql96-setup initdb
- 启用数据库并设置开机自动启动
systemctl enable postgresql-9.6 systemctl start postgresql-9.6
- 设置环境变量: 编辑/etc/profile文件
PGHOST=127.0.0.1 PGDATABASE=postgres PGUSER=postgres PGPORT=5432 PATH=/usr/pgsql-9.6/bin:$PATH export PATH export PGDATA PGHOST PGDATABASE PGUSER PGPORT export TMOUT=1000
- 修改postgres用户默认密码
1 su - postgres # 切换用户,执行后提示符会变为 '-bash-4.2$' 2 psql -U postgres # 登录数据库,执行后提示符变为 'postgres=#' 3 ALTER USER postgres WITH PASSWORD 'postgres' # 设置postgres用户密码为postgres 4 q # 退出数据库
- 设置远程访问数据库
vim /var/lib/pgsql/9.6/data/postgresql.conf
## 更改文件内容
listen_addresses = '*'
...
log_line_prefix = '%t %u %d ' - 设置信任远程连接
vim /var/lib/pgsql/9.6/data/pg_hba.conf ## 更改文件内容 # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only #local all all peer local all all trust # IPv4 local connections: host all all 127.0.0.1/32 trust host all all all trust # IPv6 local connections: host all all ::1/128 ident # Allow replication connections from localhost, by a user with the # replication privilege. #local replication postgres peer #host replication postgres 127.0.0.1/32 ident #host replication postgres ::1/128 ident
- 重启数据库
systemctl restart postgresql-9.6
- 登录数据库
12. 通过远程访问