Install PostgreSQL online
1.Installation
Official installation document PostgreSQL: Linux downloads (Red Hat family) Choose according to your needs:
- PostgreSQL的 version
- Deployment environment platform
- Select the server architecture
Copy, paste and run the relevant parts of the setup script
Example:
# 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.Use
2.1 Some psql features might not work problem handling
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 Role and password settings
# 创建角色
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 Configure remote access
# 设置监听地址
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