Last active
May 23, 2024 08:50
-
-
Save sanbei011/8da66743100221006cfb120f830b5add to your computer and use it in GitHub Desktop.
在Ubuntu22.04上安装Postgresql并配置navicat远程访问权限
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo "开始进行步骤1:安装 PostgreSQL 16..." | |
# 更新包列表并安装基本依赖 | |
sudo apt update -y | |
sudo apt install -y wget gnupg2 | |
# 添加 PostgreSQL 仓库 | |
wget -qO - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - | |
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' | |
# 更新包列表并安装 PostgreSQL 16 | |
sudo apt update -y | |
sudo apt install -y postgresql-16 postgresql-client-16 | |
# 启动并启用 PostgreSQL 服务 | |
sudo systemctl start postgresql | |
sudo systemctl enable postgresql | |
# 显示安装结果 | |
sudo -u postgres psql -c "SELECT version();" | cat | |
echo "PostgreSQL 16 安装完成。" | |
# 步骤2: 允许所有外部连接 | |
echo "开始进行步骤2: 允许所有外部连接..." | |
PGDATA="/etc/postgresql/16/main" | |
echo "PostgreSQL 数据目录: $PGDATA" | |
PG_HBA_CONF="$PGDATA/pg_hba.conf" | |
POSTGRESQL_CONF="$PGDATA/postgresql.conf" | |
echo "pg_hba.conf 路径: $PG_HBA_CONF" | |
echo "postgresql.conf 路径: $POSTGRESQL_CONF" | |
echo "正在修改 pg_hba.conf 文件..." | |
sudo bash -c "echo 'host all all 0.0.0.0/0 trust' >> $PG_HBA_CONF" | |
echo "pg_hba.conf 文件修改完成。" | |
echo "正在修改 postgresql.conf 文件..." | |
sudo sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/" $POSTGRESQL_CONF | |
echo "postgresql.conf 文件修改完成。" | |
echo "正在重启 PostgreSQL 服务以应用配置更改..." | |
sudo systemctl restart postgresql | |
echo "PostgreSQL 服务重启完成。" | |
# 步骤3: 设置 PostgreSQL 用户密码 | |
echo "开始进行步骤3: 设置 PostgreSQL 用户密码..." | |
cd /tmp | |
sudo -u postgres psql -c "ALTER USER postgres PASSWORD '@#GZH031ghr.@#';" | |
echo "步骤3: 设置 PostgreSQL 用户密码完成。" | |
echo "PostgreSQL 安装和配置完成。" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment