1、Linux下源码编译安装PostgreSQL

操作系统:Centos7

说明:postgresql必须在postgres用户下初始化数据库和启动,否则报错。

PostgreSQL的特性

  • PostgreSQL是一种几乎可以运行在各种平台上的免费的开放源码的对象关系数据库管理系统,拥有与企业级数据库相媲美的特性,如完善的SQL标准支持、多版本并发控制、时间点恢复、表空间机制、异步复制、嵌套事务、在线热备份、一个复杂的查询优化器、预写日志容错技术。

  • 它支持国际字符集、多字节字符编码、Unicode,并且对格式化、排序、大小写敏感提供本地化支持。

  • PostgreSQL在管理大数据方面有良好的可扩展性,对并发用户管理具有自适应性。现在已经出现具有管理超过4万亿字节数据能力的实用版本产品。

  • 开放特性:PostgreSQL内置了丰富的数据类型,如任意精度的数值、无限制长度的文本、几何图元、IP地址、数组等;同时还允许用户定义基于正规SQL类型的新类型,让数据库自身理解复杂数据,自定义类型中还可以包含继承关系。用户可以为数据库内几乎所有的对象定义新的类型,如索引、操作符(可重载现有操作符)、聚集函数、数据域、数据类型转换、会话(编码转换)等。

  • 可编程性:PostgreSQL同样拥有大量的编程接口供用户开发使用,如ODBC、JDBC(Java)、Libpq(C/C++)等。

  • 可定制性:PostgreSQL拥有广泛的编程语言支持来实现函数功能,包括内置的PL/PGSQL过程语言,PL/Perl、PL/PHP、PL/Python、PL/Ruby、PL/Tcl等脚本语言,以及Java、C/C++等高级编程语言。

  • 函数的输出是一系列行类型的集合,可以在查询中当做表来使用,函数也可以被定义成以创建者或者调用者的身份运行。在其他的数据库产品中,函数也会被称为“存储过程”。

  • 索引手段:用户可以自定义索引方法或者使用内置B-Tree索引、Hash表索引、GiST索引、GIN索引。GiST索引不是某种特定的索引类型,而是一种通用索引基础结构,可以在这种结构上实现很多不同的索引策略。

  • PostgreSQL同时还支持如下功能:反向索引检索、表达式索引、部分索引、位图索引扫描。

  • 多种身份认证方式:PostgreSQL中可以使用数据库用户/角色、操作系统、PAM、Kerberos等方式,根据配置文件(pg_hba.conf)中的设置执行对应的身份认证。

  • PostgreSQL提供经济有效的、易于部署的复杂数据管理基础设施,在工业界、互联网得到广泛应用,在许多国际化大公司的应用中取得了良好的效果。

  • PostgreSQL经过多年的发展,得到了学术界和工业界的充分认可。获得2008 Developer.com编辑选择的数据库工具方向的年度产品,2000年、2003~2006年荣获Linux Journal杂志编辑评选的“最佳数据库”奖,2004年获ArsTechnica最佳服务器应用奖,2002年获Linux New Media杂志编辑评选的“最佳数据库”奖,1999年获Linux World杂志评选的“最佳数据库”奖等多项荣誉。

下载源码包

https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.gz

解压

# mkdir -p /application/
# mkdir -p /tools/
# cd /tools/
# tar -xzf postgresql-12.2.tar.gz
# cd postgresql-12.2/

 

安装依赖包

 # yum install readline-devel -y
 # yum install zlib-devel -y

 

指定安装路径

  # ./configure --prefix=/application/postgresql-12.2/

编译安装

# make && make install
  # ln -s /application/postgresql-12.2/ /application/postgresql

用户权限

创建虚拟用户

# useradd -M -s /sbin/nologin postgres
  # id postgres
  # uid=1000(postgres) gid=1000(postgres) 组=1000(postgres)
  说明:创建postgres用户为虚拟用户时,因没有家目录,会报“could not change directory to "/root": 权限不够”的提示,忽略即可, 在postgres用户有家目录的情况下,不会报该提示。

创建数据库文件目录

# mkdir /application/postgresql-12.2/data/

用户文件访问权限变更

# chown -R postgres.postgres /application/postgresql/

 

添加环境变量
vim /etc/profile
export PGHOME=/application/postgresql
export PGDATA=/application/postgresql/data
export PATH=$PGHOME/bin:$PATH
source /etc/profile#说明:PGDATA:环境变量在postgresql.conf配置文件中会被引用
用户切换
# su - postgres -s /bin/bash

数据库初始化

# initdb -D /application/postgresql/data/
image_5ebad035_55f3

配置修改

配置数据库允许访问的IP

vim /application/postgresql/data/pg_hba.conf

配置IP都可以连接数据库(如需要所有IP都可以访问则对应为0.0.0.0/0),此处认证类型选择的是MD5,可参考官方文档认证类型,选择适合的认证方式

 

# TYPE  DATABASE        USER            ADDRESS                 METHOD# "local" is for Unix domain socket connections onlylocalallall                                     trust
# IPv4 local connections:hostallall             127.0.0.1/32            trust
# IPv6 local connections:hostallall             ::1/128                 trust
# Allow replication connections from localhost, by a user with the# replication privilege.local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust
host    all             all             0.0.0.0/0               md5
说明:
TYPE:pg的连接方式,local:本地unix套接字,host:tcp/ip连接
DATABASE:指定数据库
USER:指定数据库用户
ADDRESS:ip地址,可以定义某台主机或某个网段,32代表检查整个ip地址,相当于固定的ip,24代表只检查前三位,最后一位是0~255之间的任何一个
METHOD:认证方式,常用的有identmd5passwordtrustrejectmd5是常用的密码认证方式。
                      password是以明文密码传送给数据库,建议不要在生产环境中使用。
                      trust是只要知道数据库用户名就能登录,建议不要在生产环境中使用。
                      reject是拒绝认证。

配置监听地址、连接端口号、打开日志。

# vim /application/postgresql/data/postgresql.conf

listen_addresses配置监听地址范围,改为*则为所有

port 默认为5432

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = '*'                  # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
port = 5432                             # (change requires restart)
#------------------------------------------------------------------------------
# REPORTING AND LOGGING
#------------------------------------------------------------------------------
# - Where to Log -
log_destination = 'stderr'              # Valid values are combinations of
                                        # stderr, csvlog, syslog, and eventlog,
                                        # depending on platform.  csvlog
                                        # requires logging_collector to be on.
# This is used when logging to stderr:
logging_collector = on                  # Enable capturing of stderr and csvlog
                                        # into log files. Required to be on for
                                        # csvlogs.
                                        # (change requires restart)
# These are only used if logging_collector is on:
log_directory = 'log'                   # directory where log files are written,
                                        # can be absolute or relative to PGDATA
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,

启动数据库服务

# pg_ctl start
# 关闭数据库服务为 pg_ctl stop
  # -bash-4.2$ psql -V
  image_5ebad035_612a
# -bash-4.2$ netstat -tunlp | grep 5432
image_5ebad035_6ad

修改密码

# psql
  # 使用 pgsql 命令登录控制台,默认数据库服务地址为 -h 127.0.0.1,默认端口号 -p 5432,默认用户名 -U postgres,默认数据库 -d postgres
  # 又因为在 pg_hba.conf 中设置了 127.0.0.1 的远程 ip 地址登录为无密码验证,所以可以直接登录到控制台。
 
# password postgres
# 修改数据库 postgres 用户密码


使用 Navicat Premium 远程连接 postgreSQL 数据库
image_5ebad036_1a1f
 
# -sh-4.2$ psql --help
could not change directory to "/root": 权限不够
psql is the PostgreSQL interactive terminal.
Usage:
  psql [OPTION]... [DBNAME [USERNAME]]
 
General options:
  -c, --command=COMMAND    run only single command (SQL or internal) and exit
  -d, --dbname=DBNAME      database name to connect to (default: "postgres")
  -f, --file=FILENAME      execute commands from file, then exit
  -l, --list              list available databases, then exit
  -v, --set=, --variable=NAME=VALUE
                          set psql variable NAME to VALUE
                          (e.g., -v ON_ERROR_STOP=1)
  -V, --version            output version information, then exit
  -X, --no-psqlrc          do not read startup file (~/.psqlrc)
  -1 ("one"), --single-transaction
                          execute as a single transaction (if non-interactive)
  -?, --help[=options]    show this help, then exit
      --help=commands      list backslash commands, then exit
      --help=variables    list special variables, then exit
 
Input and output options:
  -a, --echo-all          echo all input from script
  -b, --echo-errors        echo failed commands
  -e, --echo-queries      echo commands sent to server
  -E, --echo-hidden        display queries that internal commands generate
  -L, --log-file=FILENAME  send session log to file
  -n, --no-readline        disable enhanced command line editing (readline)
  -o, --output=FILENAME    send query results to file (or |pipe)
  -q, --quiet              run quietly (no messages, only query output)
  -s, --single-step        single-step mode (confirm each query)
  -S, --single-line        single-line mode (end of line terminates SQL command)
 
Output format options:
  -A, --no-align          unaligned table output mode
      --csv                CSV (Comma-Separated Values) table output mode
  -F, --field-separator=STRING
                          field separator for unaligned output (default: "|")
  -H, --html              HTML table output mode
  -P, --pset=VAR[=ARG]    set printing option VAR to ARG (see pset command)
  -R, --record-separator=STRING
                          record separator for unaligned output (default: newline)
  -t, --tuples-only        print rows only
  -T, --table-attr=TEXT    set HTML table tag attributes (e.g., width, border)
  -x, --expanded          turn on expanded table output
  -z, --field-separator-zero
                          set field separator for unaligned output to zero byte
  -0, --record-separator-zero
                          set record separator for unaligned output to zero byte
 
Connection options:
  -h, --host=HOSTNAME      database server host or socket directory (default: "local socket")
  -p, --port=PORT          database server port (default: "5432")
  -U, --username=USERNAME  database user name (default: "postgres")
  -w, --no-password        never prompt for password
  -W, --password          force password prompt (should happen automatically)
原文地址:https://www.cnblogs.com/LiuChang-blog/p/12879891.html