在 Ubuntu 上用 pgAdmin 连接 PostgreSQL,一般分以下几步:
sudo apt update
sudo apt install postgresql postgresql-contrib
安装后默认会创建一个 postgres 系统用户和数据库用户。
启动并设置开机自启:
sudo systemctl start postgresql
sudo systemctl enable postgresql
sudo apt install pgadmin4
安装完成后,在菜单中搜索 pgAdmin 4 打开即可。
如果是服务器无界面环境,建议用 pgAdmin 4 Web 模式 或命令行
psql。
编辑配置文件:
sudo nano /etc/postgresql/*/main/postgresql.conf
找到:
#listen_addresses = 'localhost'
改为:
listen_addresses = 'localhost'
# 或允许远程
listen_addresses = '*'
sudo nano /etc/postgresql/*/main/pg_hba.conf
本地连接一般已有:
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
如果是远程连接,添加:
host all all 0.0.0.0/0 md5
修改后重启:
sudo systemctl restart postgresql
sudo -u postgres psql
CREATE USER myuser WITH PASSWORD 'mypassword';
CREATE DATABASE mydb OWNER myuser;
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
\q
LocalPG)127.0.0.15432postgresmyuser(或 postgres)✅ 连接成功!
listen_addresses 没改对ufw)没放行 5432pg_hba.conf 用了 peer 而不是 md5如果你是想 远程连接(比如 Windows pgAdmin 连 Ubuntu 服务器),或者 Docker 里的 PostgreSQL,可以告诉我,我给你对应方案。