PostgreSQL安装、使用

一、安装

1.在官网选择对应系统的版本:

《PostgreSQL安装、使用》

2.使用yum安装

  • Install the repository RPM:
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm 
  • Install the client packages:
yum install postgresql96
  • Optionally install the server packages:
yum install postgresql96-server
  • Optionally initialize the database :
/usr/pgsql-9.6/bin/postgresql96-setup initdb

3.启动、停止服务

systemctl start postgresql-9.6
systemctl stop postgresql-9.6
systemctl restart postgresql-9.6

二、配置(修改配置文件需要重启服务)

1. 修改用户密码

  • yum安装postgresql,默认会建一个名为”postgres”的系统账号,用于执行PostgreSQL;
  • 同时数据库中也会生成一个名为”postgres”的数据库用户,且密码已自动生成,需要进入数据库后修改;
  • PostgreSQL在数据库用户同名的系统账号下登录免密。
su - postgres
alter user postgres with password '123456'

2. 允许远程访问

  • 配置文件中,默认只能本机访问postgresql;
  • 修改listen_addresses = ‘localhost’为listen_addresses = ‘*’,允许所有远程访问。
sed -i "s|#listen_addresses = 'localhost'|listen_addresses = '*'|g" /var/lib/pgsql/9.6/data/postgresql.conf

3. 添加主机认证

  • 在第82行之后,”IPv4 local connections”下新增允许的客户端;
vim /var/lib/pgsql/9.6/data/pg_hba.conf
host    all             all             172.26.126.26/32          trust

“host” 代表主机类型,
第一个“all”代表db,
第二个“all”代表user ,
“172.26.126.26/32” 代表client ip,“trust”代表认证方式;
认证方式除“trust”外,还有“peer”, “ident”, “md5”, “password”等,具体可参考pg-hba文件: https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html

    原文作者:数大招疯
    原文地址: https://www.jianshu.com/p/6c01e3f770bc
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞