在 Linux 下使用 pgAdmin 连接 PostgreSQL 数据库,一般流程如下(适用于 pgAdmin 4,Web 版):
确认 PostgreSQL 已安装并运行:
psql --version
启动数据库服务(以 systemd 为例):
sudo systemctl start postgresql
sudo systemctl enable postgresql
检查状态:
sudo systemctl status postgresql
Ubuntu / Debian:
sudo apt update
sudo apt install pgadmin4
CentOS / Rocky / Alma:
sudo dnf install pgadmin4
python3 -m venv pgadmin4
source pgadmin4/bin/activate
pip install pgadmin4
启动:
pgadmin4
浏览器访问:
http://127.0.0.1:5050
pg_hba.conf路径通常在:
/var/lib/pgsql/data/pg_hba.conf
# 或
/etc/postgresql/*/main/pg_hba.conf
添加或修改:
# 本地连接
local all all md5
# 远程连接(如需要)
host all all 0.0.0.0/0 md5
postgresql.conf(如需远程)listen_addresses = '*'
重启 PostgreSQL:
sudo systemctl restart postgresql
切换到 postgres 用户:
sudo -u postgres psql
创建用户和数据库:
CREATE USER myuser WITH PASSWORD 'mypassword';
CREATE DATABASE mydb OWNER myuser;
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
\q
浏览器访问:
http://localhost:5050
首次使用需设置 pgAdmin 登录密码
LocalPG)localhost 或 127.0.0.15432postgres点击 Save
could not connect to server: Connection refused
✅ 检查:
sudo systemctl status postgresql
netstat -an | grep 5432
password authentication failed
✅ 检查:
pg_hba.conf 是否使用 md5✅ 检查:
sudo ufw allow 5432
安装 PostgreSQL
启动数据库
配置 pg_hba.conf
创建用户/数据库
安装 pgAdmin4
浏览器打开 pgAdmin
添加 Server 并连接
如果你愿意,可以告诉我:
我可以给你 精确到命令级别 的解决方案。