下面按常见使用场景给你讲 PostgreSQL 的安装与基础配置,分为 Linux(Ubuntu/CentOS)、Windows、macOS,以及基础配置(用户、数据库、远程访问)。
sudo apt update
sudo apt install postgresql postgresql-contrib
安装后自动创建:
postgrespostgresql启动 / 查看状态:
sudo systemctl start postgresql
sudo systemctl enable postgresql
sudo systemctl status postgresql
sudo dnf install postgresql-server postgresql-contrib
sudo postgresql-setup --initdb
sudo systemctl start postgresql
sudo systemctl enable postgresql
psql -U postgresbrew install postgresql
brew services start postgresql
连接测试:
psql postgres
sudo -i -u postgres
psql
ALTER USER postgres PASSWORD 'your_password';
-- 创建用户
CREATE USER myuser WITH PASSWORD 'mypassword';
-- 创建数据库
CREATE DATABASE mydb OWNER myuser;
-- 授权
GRANT ALL PRIVILEES ON DATABASE mydb TO myuser;
postgresql.conf路径示例:
/etc/postgresql/15/main/postgresql.conf
修改:
listen_addresses = '*'
pg_hba.confhost all all 0.0.0.0/0 md5
或者指定 IP:
host all all 192.168.1.0/24 md5
sudo systemctl restart postgresql
# 启动
sudo systemctl start postgresql
# 停止
sudo systemctl stop postgresql
# 登录
psql -U username -d dbname -h host
# 查看数据库
\l
# 查看表
\dt
✅ 连接被拒绝
listen_addresses✅ peer authentication failed
sudo -u postgres psql
如果你告诉我:
我可以给你更针对性的配置方案。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。