PostgreSQL 名称来源

It was originally named POSTGRES, referring to its origins as a successor to the Ingres database developed at the University of California, Berkeley.

In 1996, the project was renamed to PostgreSQL to reflect its support for SQL.

PostgreSQL 的发音为 [ˈpəʊsɡreˈsɪkl], 中间部分类似于 progress 的发音

安装

参考官方安装说明 https://www.postgresql.org/download/linux/ubuntu/

# 创建软件源
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# 添加key
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# 安装
sudo apt update
sudo apt install postgresql
# 检查
sudo systemctl status postgresql
# 查看端口
sudo netstat -lnp

安装后, 系统中会增加一个 postgres 用户, 注意哦, 这个用户是可以登录的. 建议给这个用户设个密码

$ more /etc/passwd
...
postgres:x:113:121:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash

这个用户可以直接访问 postgresql

$ sudo su postgres
[sudo] password for milton:
postgres@ubuntu:/home/milton$ psql
psql (14.2 (Ubuntu 14.2-1.pgdg20.04+1))
Type "help" for help. postgres=#
postgres-# \q
postgres@ubuntu:/home/milton$

配置

查看数据库列表

sudo -u postgres psql -l

创建用户

# 使用 postgres 用户
createuser --interactive
# 或者 sudo
sudo -u postgres createuser --interactive

默认情况下, postgresql 会使用与linux用户同名的用户登录, 所以如果创建了用户为 milton, 还需要再创建一个名为 milton 的 linux 用户.

创建数据库

# 使用 postgres 用户
createdb milton
# 或者 sudo
sudo -u postgres createdb milton
# 指定用户
sudo -u postgres createdb testdb -O postgres

主配置

对应的配置文件在 /etc/postgresql//main, 当前的版本是14, 路径是 /etc/postgresql/14/main/postgresql.conf

sudo vi /etc/postgresql/14/main/postgresql.conf

主配置文件说明 https://www.postgresql.org/docs/14/runtime-config-connection.html

服务IP listen_addresses

# 监听所有地址
listen_addresses = '*'
# 监听指定地址
listen_addresses = '192.168.10.20'

服务端口 port

port = 5432

密码加密方式 password_encryption

password_encryption = scram-sha-256    # scram-sha-256 or md5

用户名命名空间 db_user_namespace, 如果设置为on, 用户创建时可以使用 username@dbname 这样的格式, 用于与数据库绑定. 这时候依然可以创建全局用户, 但是连接时客户端必须加上 @

db_user_namespace = off

基于主机的认证配置

配置文件 pg_hba.conf, 配置说明 https://www.postgresql.org/docs/14/auth-pg-hba-conf.html

客户端认证由配置文件控制, 通常为名为 pg_hba.conf 的文件, 存储在集群的数据目录(HBA 代表 host-based authentication 的缩写). 当数据目录初始化时, 会生成一个默认的 pg_hba.conf 文件. 可以通过修改主配置文件, 将文件放到其他路径.

pg_hba.conf 文件通常的格式是按行组织的文本记录

  • 使用#号标识注释
  • 如果一行未结束需要换行, 使用\符号.
  • 每行记录由一些空格或tab分隔的字段组成. 如果字段包含空格, 需要用双引号包围.
  • 每行记录指定了: 连接类型, 客户端IP范围, 数据库名, 用户名, 验证方式.
  • 匹配的第一个记录(匹配连接类型+客户端地址+数据库+用户名)将用于验证
  • 没有缺省或再次验证, 只要一个记录被选中, 那么验证就只用这个记录处理, 如果没有命中的记录, 就返回拒绝.

初始配置示例

# 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 scram-sha-256
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256

记录格式说明

记录可以是这些格式

local         database  user  auth-method [auth-options]
host database user address auth-method [auth-options]
hostssl database user address auth-method [auth-options]
hostnossl database user address auth-method [auth-options]
hostgssenc database user address auth-method [auth-options]
hostnogssenc database user address auth-method [auth-options]
host database user IP-address IP-mask auth-method [auth-options]
hostssl database user IP-address IP-mask auth-method [auth-options]
hostnossl database user IP-address IP-mask auth-method [auth-options]
hostgssenc database user IP-address IP-mask auth-method [auth-options]
hostnogssenc database user IP-address IP-mask auth-method [auth-options]

连接方式

  • local 使用本机Unix-domain sockets, 如果没有local开头的记录, 则不允许用Unix-domain sockets连接
  • host 使用TCP/IP连接, 包含SSL和GSSAPI方式
  • hostssl TCP/IP + 使用SSL
  • hostnossl TCP/IP + 不使用SSL
  • hostgssenc TCP/IP + GSSAPI 加密
  • hostnogssenc TCP/IP + 不使用 GSSAPI 加密

数据库, 指定匹配的数据库

  • 数据库名 指定数据库, 多个数据库使用逗号连接
  • all 匹配所有
  • sameuser 与此数据库同名的用户, 必须是这个用户
  • samerole 与此数据库同名的role, 用户必须属于这个role
  • samegroup 以废弃
  • replication
  • @ 可以用@号指定文件

用户, 指定匹配的用户

  • 用户名 指定的用户, 多个用户用+号连接
  • all 所有用户
  • @ 可以用@号指定文件

客户端地址

  • 172.20.143.89/32 IPv4地址或范围
  • 172.20.1.1/255.0.0.0 IPv4地址范围的另一种格式
  • fe80::7a31:c1ff:0000:0000/96 IPv6地址或范围
  • all 所有地址
  • samenet 同一子网的地址
  • samehost 当前主机的所有地址
  • .example.com 域名通配

验证方式

  • trust 无条件通过
  • reject 直接拒绝
  • scram-sha-256 使用SCRAM-SHA-256验证
  • md5 使用 Perform SCRAM-SHA-256 或 MD5 验证
  • password 使用未加密的密码验证, 注意这种方式下, 密码在网络中是明文传输
  • gss 使用 GSSAPI 验证, 仅适用于 TCP/IP 连接.
  • sspi 使用 SSPI 验证, 仅适用于 Windows
  • ident 通过ident服务器, 获取当前客户端操作系统用户名, 并与请求的数据库用户名进行校验, 仅适用于 TCP/IP 连接.
  • peer 从操作系统获取用户名, 仅适用于 local 方式的连接
  • ldap Authenticate using an LDAP server.
  • radius Authenticate using a RADIUS server
  • cert 使用 SSL 客户端证书进行验证
  • pam 使用操作系统提供的 Pluggable Authentication Modules (PAM) 服务进行验证
  • bsd 使用操作系统提供的 BSD Authentication service 进行验证

验证选项

  • 根据不同的验证方式提供的选项

常用命令

修改用户口令

postgres=# ALTER USER postgres WITH PASSWORD 'postgres';

待更新补充

Ubuntu20.04 PostgreSQL 14 安装配置记录的更多相关文章

  1. 自动化kolla-ansible部署ubuntu20.04+openstack-victoria之基础配置-04

    自动化kolla-ansible部署ubuntu20.04+openstack-victoria之基础配置-04 欢迎加QQ群:1026880196 进行交流学习 近期我发现网上有人转载或者复制原创博 ...

  2. Angularjs学习---ubuntu12.04中karma安装配置

    Angularjs学习---ubuntu12.04中karma安装配置中常见的问题总结   karma启动时出现了很多问题: 1.安装karma前提条件 安装karma首先要安装nodejs,npm然 ...

  3. mysql 5.7.14 安装配置方法图文教程(转)

    http://www.jb51.net/article/90259.htm ******************************** 因笔者个人需要需要在本机安装Mysql,先将安装过程记录如 ...

  4. 【linux】Ubuntu20.04使用apt安装tomcat9

    Ubuntu20.04使用apt安装tomcat9 前言 系统环境:ubuntu20.04 java版本:openjdk version "11.0.11" 2021-04-20 ...

  5. 在Ubuntu 12.04系统中安装配置OpenCV 2.4.3的方法

    在Ubuntu 12.04系统中安装配置OpenCV 2.4.3的方法   对于,在Linux系统下做图像识别,不像在windows下面我们可以利用Matlab中的图像工具箱来实现,我们必须借助Ope ...

  6. 【Ubuntu 16.04.2_64】安装配置SVN

    [Ubuntu 16.04.2_64]安装配置SVN 转载:https://www.cnblogs.com/yangchongxing/p/10190549.html 检查是否已安装svn # svn ...

  7. 自动化kolla-ansible部署ubuntu20.04+openstack-victoria之文件配置-08

    自动化kolla-ansible部署ubuntu20.04+openstack-victoria之文件配置-08 欢迎加QQ群:1026880196   进行交流学习 文件配置 #controller ...

  8. Ubuntu 14.04 java环境安装配置(不是openJAVA)

    两种配置方式 第一: 在 Ubuntu 中使用 PPA 安装 Java 8 ( 支持 Ubuntu 10.04 - Ubuntu 14.04 ): sudo add-apt-repository pp ...

  9. MySQL学习笔记(一)Ubuntu16.04中MySQL安装配置(5.6优化、错误日志、DNS解决)

    目录 第一部分.5.6安装.配置.自动备份 第二部分.5.7源码安装.配置.自动备份 第一部分.5.6安装 1.安装mysql sudo apt-get install mysql-server su ...

随机推荐

  1. idea 创建Maven项目,Enable auto Import报“本地服务器没有从权威服务器上收到响应”

    完整的报错信息:Could not transfer artifact org.apache.maven.plugins:maven-clean-plugin:pom:2.5 from/to cent ...

  2. linux 之 导出远程oracle数据库表结构及数据

    导出用户下所有表结构及数据 exp 用户名/密码@ip/数据库 file=文件路径/文件名.dmp owner='用户' 导出用户下所有表结构,不导出数据 exp 用户名/密码@ip/数据库 file ...

  3. c# - 关于位移符号 >> 和 << 的使用

    1.前言 这是对二进制数据进行位移的方法 2.操作 using System; namespace ConsoleApp1.toValue { public class test1 { public ...

  4. Golang实现集合(set)

    package set package set import ( "bytes" "fmt" "sync" ) type Set struc ...

  5. Presto 在字节跳动的内部实践与优化

    在字节跳动内部,Presto 主要支撑了 Ad-hoc 查询.BI 可视化分析.近实时查询分析等场景,日查询量接近 100 万条.本文是字节跳动数据平台 Presto 团队-软件工程师常鹏飞在 Pre ...

  6. python+selenium 元素定位--iframe

    1. 一般webdriver要操作页面元素需要在Top Window的状态下,如下: 2.当浏览器显示iframe时,用正常的元素定位是没有效果的,需要将页面装换到iframe下再对页面元素进行操作 ...

  7. 手写Webserver

    一.反射 反射Reflection:把java类中的各种结构(方法.属性.构造器.类名)映射成一个个的java对象.利用反射技术可以对一个类进行解剖,反射是框架设计的灵魂 //在运行期间,一个类,只有 ...

  8. Keil MDK STM32系列(八) STM32F4基于HAL的PWM和定时器输出音频

    Keil MDK STM32系列 Keil MDK STM32系列(一) 基于标准外设库SPL的STM32F103开发 Keil MDK STM32系列(二) 基于标准外设库SPL的STM32F401 ...

  9. 【解决了一个问题】腾讯云中使用ckafka生产消息时出现“kafka server: Message contents does not match its CRC.”错误

    初始化的主要代码如下: config := sarama.NewConfig() config.Producer.RequiredAcks = sarama.WaitForAll // Wait fo ...

  10. C++11多线程之future(一)

    // ConsoleApplication5.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include<random> #in ...