Ubuntu 安装配置最新版 PostgreSQL
环境:Ubuntu Xenial (16.04) !!! CentOS 参考这里
#安装 PostgreSQL
sudo apt-get update
sudo apt-get upgrade
apt-get install htop wget software-properties-common
默认: sudo apt-get install postgresql (9.5版本)
For any Ubuntu version you need to can do this by the official PostgreSQL Apt Repository.
Ubuntu Xenial (16.04)
sudo add-apt-repository "deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main"
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.6
Ubuntu Trusty (14.04)sudo add-apt-repository "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main"
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.6
Ubuntu Precise (12.04)sudo add-apt-repository "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main"
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.6
# 配置 PostgreSQL
安装完成后,默认会:
(1)创建名为"postgres"的Linux用户
(2)创建名为"postgres"、不带密码的默认数据库账号作为数据库管理员
(3)创建名为"postgres"的表
默认配置信息:
Creating new cluster 9.6/main ...
config /etc/postgresql/9.6/main
data /var/lib/postgresql/9.6/main
locale en_US.UTF-8
socket /var/run/postgresql
port 5432
# PostgreSQL 命令
#查看版本
psql -- version
#切换用户
sudo -u postgres psql
#修改密码
ALTER USER postgres WITH ENCRYPTED PASSWORD 'XXX';
修改密码本地环境生效配置
#修改pg_hba.conf
#local all postgres peer
local all postgres md5
#重启服务
sudo service postgresql restart
#登陆
psql -U postgres
#修改Linux用户的密码
sudo passwd postgres
#配置数据库以允许远程连接访问
sudo vi /etc/postgresql/9.4/main/postgresql.conf
# 增加 listen_addresses = '*'
sudo vi /etc/postgresql/9.4/main/pg_hba.conf
#修改 IP4
#host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5

# DO NOT DISABLE! # If you change this first entry you will need to make sure that the # database superuser can access the database using some other method. # Noninteractive access to all databases is required during automatic # maintenance (custom daily cronjobs, replication, and similar tasks). # # Database administrative login by Unix domain socket local all postgres peer # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all peer # IPv4 local connections: #host all all 127.0.0.1/32 md5 host all all 0.0.0.0/0 md5 # IPv6 local connections: host all all ::1/128 md5 # Allow replication connections from localhost, by a user with the # replication privilege. #local replication postgres peer #host replication postgres 127.0.0.1/32 md5 #host replication postgres ::1/128 md5
#重启服务
sudo service postgresql restart
sudo /etc/init.d/postgresql restart (reload)
\password:设置密码
\q:退出
\h:查看SQL命令的解释,比如\h select。
\?:查看psql命令列表。
\l:列出所有数据库。
\c [database_name]:连接其他数据库。
\d:列出当前数据库的所有表格。
\d [table_name]:列出某一张表格的结构。
\du:列出所有用户。
\e:打开文本编辑器。
\conninfo:列出当前数据库和连接的信息。
#防火墙
启用ufw: sudo ufw enable
防外对内访问:sudo ufw default deny
关闭:sudo ufw disable
状态:sudo ufw status
#查看TCP或者UDP端口使用情况
sudo netstat -anp
#某个端口的信息
sudo lsof -i :631
#查看Linux的端口使用情况
netstat -tln
#查看已经连接的服务端口(ESTABLISHED)
netstat -a
#查看所有的服务端口(LISTEN,ESTABLISHED)
sudo netstat -ap
REFER:
https://www.pgadmin.org/download/
MySQL、Postgres 开启远程访问权限(ubuntu)
http://wenzhixin.net.cn/2012/05/15/mysql_open_the_remote_access_ubuntu
Omnibus-GitLab 配置 PostgreSQL 开启远程访问
https://cong5.net/post/open-remote-connect-postgresql-with-omnibus-gitlab
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述