ubuntu安装PostgreSQL

1、安装数据库

# 更新软件包列表并安装
sudo apt update
sudo apt install postgresql postgresql-contrib -y

  # 安装特定版本(以PostgreSQL 14为例)
  sudo apt install postgresql-14 postgresql-client-14 -y

2、远程访问

文件类型    默认路径(PostgreSQL 14)
主配置文件    /etc/postgresql/14/main/postgresql.conf
访问控制文件    /etc/postgresql/14/main/pg_hba.conf

  # 修改postgresql.conf
  listen_addresses = '*' # 允许所有IP连接

  # 修改pg_hba.conf添加规则
  host all all 0.0.0.0/0 md5

  重启服务

   sudo systemctl restart postgresql

3、修改默认密码

# 切换到postgres系统用户
sudo -i -u postgres
psql

-- 登录后修改密码
ALTER USER postgres PASSWORD '新密码';
 

4、命令

# 启动服务
sudo systemctl start postgresql

# 停止服务
sudo systemctl stop postgresql

# 重启服务
sudo systemctl restart postgresql

# 重新加载配置(不重启)
sudo systemctl reload postgresql
# 查看服务状态
sudo systemctl status postgresql

# 查看详细状态信息
sudo systemctl status postgresql -l

# 查看服务是否开机自启
sudo systemctl is-enabled postgresql

# 查看服务是否正在运行
sudo systemctl is-active postgresql

# 启用开机自启动
sudo systemctl enable postgresql

# 禁用开机自启动
sudo systemctl disable postgresql

# 立即启用并启动服务
sudo systemctl enable --now postgresql

# 重新启用服务
sudo systemctl reenable postgresql
 

 

posted @ 2025-12-25 23:49  lzy1666  阅读(4)  评论(0)    收藏  举报