温馨提示×

centos postgresql使用教程

小樊
51
2025-08-31 02:13:02
栏目: 云计算

以下是一份CentOS下PostgreSQL的使用教程:

安装

  1. 添加官方仓库:sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %{rhel})-x86_64/pgdg-redhat-repo-latest.noarch.rpm
  2. 禁用系统自带模块(可选):sudo dnf -qy module disable postgresql
  3. 安装PostgreSQL:sudo yum install -y postgresql16-server postgresql16(以16版本为例)。
  4. 初始化数据库:sudo /usr/pgsql-16/bin/postgresql-16-setup initdb
  5. 启动服务并设置开机自启:sudo systemctl start postgresql-16sudo systemctl enable postgresql-16

配置

  1. 若需远程访问,编辑/var/lib/pgsql/16/data/postgresql.conf,将listen_addresses设为'*'
  2. 编辑/var/lib/pgsql/16/data/pg_hba.conf,添加host all all 0.0.0.0/0 md5以允许所有IP用MD5认证访问。
  3. 重启服务使配置生效:sudo systemctl restart postgresql-16

创建用户和数据库

  1. 切换到postgres用户:sudo -i -u postgres
  2. 创建数据库:createdb your_database_name
  3. 创建用户:CREATE USER your_username WITH PASSWORD 'your_password'
  4. 授权:GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_username

连接数据库

本地连接:psql -U postgres;远程连接:psql -U your_username -d your_database -h your_host -p your_port

0