PostgreSQL

The world’s most advanced open source database.

安装

官方源中版本过旧,使用 PostgreSQL 官方源

  1. sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
  2. # 禁用不需要的仓库
  3. sudo yum-config-manager --disable pgdg*
  4. sudo yum-config-manager --enable pgdg-common pgdg12
  5. sudo yum install -y postgresql12-server
  6. # Initialize the database
  7. sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
  8. sudo systemctl enable postgresql-12
  9. sudo systemctl start postgresql-12

创建用户

默认只有一个 postgres 用户,为方便使用,可添加一个与系统用户同名的新用户

  1. sudo -u postgres -i
  2. createuser --interactive
  3. # 创建普通用户
  4. createuser --no-createdb --no-createrole --no-superuser --pwprompt --interactive
  5. # 创建数据库
  6. createdb --owner USER_NAME DATABASE_NAME

配置

允许远程连接

修改 /var/lib/pgsql/12/data/postgresql.conf:

  1. listen_addresses = '*'

trust 认证

默认的验证方式是 ident,为方便本地应用连接,可考虑改为 trust

修改 /var/lib/pgsql/12/data/pg_hba.conf:

  1. host all all 127.0.0.1/32 trust
  2. host all all ::1/128 trust

密码认证

修改 /var/lib/pgsql/12/data/pg_hba.conf:

  1. # 出于安全考虑,建议只允许指定数据库或用户远程连接,尽量不要允许默认的管理员帐号 postgres 远程连接
  2. host all all 0.0.0.0/0 password

参考资料