要在 Linux 上连接并使用 PgAdmin 管理 PostgreSQL,一般分为 安装 PgAdmin、配置 PostgreSQL 和 建立连接 三个步骤。下面按常见场景详细说明。
sudo apt update
sudo apt install curl ca-certificates
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /usr/share/keyrings/postgresql-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/postgresql-keyring.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
sudo apt update
sudo apt install pgadmin4
sudo /usr/pgadmin4/bin/setup-web.sh
按提示设置 PgAdmin 登录邮箱和密码。
浏览器访问:
http://localhost/pgadmin4
sudo apt install python3-venv
python3 -m venv pgadmin4
source pgadmin4/bin/activate
pip install pgadmin4
pgadmin4
pg_hba.confsudo nano /etc/postgresql/15/main/pg_hba.conf
添加或修改:
# 本地连接
local all all md5
# 远程连接(如需要)
host all all 0.0.0.0/0 md5
postgresql.confsudo nano /etc/postgresql/15/main/postgresql.conf
确保:
listen_addresses = '*'
sudo systemctl restart postgresql
http://localhost/pgadmin4local-postgres)localhost 或 127.0.0.15432postgrespostgres✅ 检查:
sudo systemctl status postgresql
✅ 检查:
sudo ufw allow 5432
sudo -u postgres psql
\password postgres
安装 PgAdmin
↓
配置 PostgreSQL(pg_hba.conf / postgresql.conf)
↓
启动 PostgreSQL
↓
PgAdmin 添加 Server
↓
连接成功
如果你告诉我:
我可以给你 精确到命令级别 的教程。