centos 9 stream 上安装 postgresql 16
在 centos 9 stream 上安装 postgresql 16 可以通过以下步骤完成:
添加 postgresql 官方仓库:
postgresql 提供了 rpm 仓库,使用它可以方便地安装特定版本。
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/el-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
禁用默认的 postgresql 模块:
centos 9 stream 默认会提供系统自带的 postgresql 版本,需要禁用以避免冲突。
sudo dnf -qy module disable postgresql
安装 postgresql 16:
使用 dnf install
命令安装 postgresql 16。
sudo dnf install -y postgresql16 postgresql16-server
初始化数据库:
在第一次启动 postgresql 之前,需初始化数据库。
sudo /usr/pgsql-16/bin/postgresql-16-setup initdb
启动并启用 postgresql 服务:
将 postgresql 设置为开机自启并立即启动服务。
sudo systemctl enable postgresql-16 sudo systemctl start postgresql-16
验证安装:
可以通过查看 postgresql 版本来验证安装是否成功。
psql --version
配置防火墙(可选):
如果你需要远程访问 postgresql,请打开防火墙的 5432 端口。
sudo firewall-cmd --add-service=postgresql --permanent sudo firewall-cmd --reload
调整 postgresql 配置(可选):
编辑 pg_hba.conf
和 postgresql.conf
文件,根据需要修改监听地址、访问权限等。
配置文件路径通常为:
/var/lib/pgsql/16/data/postgresql.conf /var/lib/pgsql/16/data/pg_hba.conf
完成上述步骤后,postgresql 16 应该已经在 centos 9 stream 上成功安装并运行了。
设置密码并且远程连接
在 centos 9 stream 上安装 postgresql 16 后,可以按以下步骤连接数据库并设置密码:
切换到 postgresql 用户:
默认情况下,postgresql 会创建一个名为 postgres
的系统用户。切换到该用户,以便直接访问 postgresql 管理命令。
sudo -i -u postgres
进入 postgresql 命令行界面:
使用 psql
命令行工具连接到 postgresql。
psql
设置 postgres
用户密码:
在 psql
中,使用以下命令为 postgres
数据库用户设置密码(可以更改为你需要的密码):
alter user postgres with password 'your_secure_password';
完成后,输入 \q
退出 psql
。
配置允许远程连接(可选):
如果需要远程连接 postgresql,需要在配置文件 postgresql.conf
中设置 listen_addresses
,并在 pg_hba.conf
中调整访问权限。
修改 postgresql.conf
文件:
sudo nano /var/lib/pgsql/16/data/postgresql.conf
找到 listen_addresses
参数,将其设置为 '*'
,表示监听所有 ip 地址:
listen_addresses = '*'
修改 pg_hba.conf
文件:
sudo nano /var/lib/pgsql/16/data/postgresql.conf
在文件底部添加以下行,允许远程 ip 使用密码方式访问:
host all all 0.0.0.0/0 md5
重启 postgresql 服务:
应用新的配置。
sudo systemctl restart postgresql-16
本地测试连接:
如果要从本地使用新设置的密码连接 postgresql,可以运行以下命令:
psql -u postgres -h localhost
然后输入刚才设置的密码 your_secure_password
。
远程连接(可选):
如果启用了远程连接,可以使用 psql
或其他客户端(如 dbeaver、pgadmin)通过 ip 地址连接,示例命令如下:
psql -u postgres -h server_ip -p 5432
完成这些步骤后,postgresql 就可以通过密码进行本地或远程连接了。
到此这篇关于centos 9 stream 上安装 postgresql 16的步骤的文章就介绍到这了,更多相关centos stream安装 postgresql内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论