1.安装
官方安装文档 PostgreSQL: Linux downloads (Red Hat family) 根据需要选择:
- PostgreSQL的 version
- 部署环境的 platform
- 选择服务器的 architecture
Copy, paste and run the relevant parts of the setup script
实例:
# Install the repository RPM: sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm # Install PostgreSQL: sudo yum install -y postgresql12-server # Optionally initialize the database and enable automatic start: sudo /usr/pgsql-12/bin/postgresql-12-setup initdb sudo systemctl enable postgresql-12 sudo systemctl start postgresql-12
2.使用
2.1 Some psql features might not work 问题处理
sudo -u postgres psql [root@tcloud ~]# sudo -u postgres psql psql (9.2.24, server 12.15) WARNING: psql version 9.2, server version 12.0. Some psql features might not work. # 替换旧文件 mv /usr/bin/psql /usr/bin/psql.bak ln -s /usr/pgsql-12/bin/psql /usr/bin/psql [root@tcloud ~]# psql -version psql (12.15) Type "help" for help.
2.2 角色及密码设置
# 创建角色 CREATE ROLE admin; # 设置密码并赋权 ALTER ROLE admin WITH SUPERUSER INHERIT CREATEROLE CREATEDB LOGIN PASSWORD 'admin'; # 默认用户设置密码 ALTER USER postgres WITH PASSWORD 'postgres'; # 查询用户 SELECT * FROM pg_user; postgres=# SELECT * FROM pg_user; usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | useconfig ----------+----------+-------------+----------+---------+--------------+----------+----------+----------- admin | 16384 | t | t | f | f | ******** | | postgres | 10 | t | t | t | t | ******** | | (2 rows)
2.3 配置远程访问
# 设置监听地址 vim /var/lib/pgsql/12/data/postgresql.conf listen_addresses='*' port=5432 # 添加到配置文件 /var/lib/pgsql/12/data/pg_hba.conf echo "local all admin trust" >> /var/lib/pgsql/12/data/pg_hba.conf echo "host all admin 0.0.0.0/0 md5" >> /var/lib/pgsql/12/data/pg_hba.conf echo "local all postgres trust" >> /var/lib/pgsql/12/data/pg_hba.conf echo "host all postgres 0.0.0.0/0 md5" >> /var/lib/pgsql/12/data/pg_hba.conf # 重启 sudo systemctl restart postgresql-12