PostgreSQL 数据库安装手册

本手册基于 Windows 下的 wsl (CentOS Linux release 7.9.2009 (Core)) 操作系统安装,其他环境请酌情参考


下载数据库

数据库:https://www.postgresql.org

资源地址:https://www.postgresql.org/ftp/source/

使用的版本:https://www.postgresql.org/ftp/source/v10.19/

下载链接:https://ftp.postgresql.org/pub/source/v10.19/postgresql-10.19.tar.gz

安装数据库

安装依赖

yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake

解压软件

tar -xzvf postgresql-10.19.tar.gz

创建安装文件夹

[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# mkdir postgresql
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# ll
total 24636
drwxr-xr-x 2 root root     4096 Dec 11 17:54 postgresql
drwxrwxrwx 6 1107 1107     4096 Nov  9  2021 postgresql-10.19
-rw-r--r-- 1 root root 25216304 Dec 11 17:46 postgresql-10.19.tar.gz
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]#

配置文件路径

# 获取编译路径
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# cd ./postgresql
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# pwd
/home/data/soft/postgresql/postgresql
# 开始编译
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# cd ../postgresql-10.19
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# ls
aclocal.m4  config  configure  configure.in  contrib  COPYRIGHT  doc  GNUmakefile.in  HISTORY  INSTALL  Makefile  README  src
# 配置文件生成路径
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# ./configure  --prefix=/home/data/soft/postgresql/postgresql
# 一大堆,不粘了,没报错就行
选项 描述
–prefix=prefix 安装到prefix指向的目录;默认为/usr/local/pgsql
–bindir=dir 安装应用程序到dir;默认为prefix/bin
–with-docdir=dir 安装文档到dir;默认为prefix/doc
–with-pgport=port 设置默认的服务器端网络连接服务TCP端口号
–with-tcl 为服务端提供Tcl存储过程支持
–with-perl 为服务端提供Perl存储过程支持
–with-python 为服务端提供Python存储过程支持

编译

# 这俩命令执行过程也都一大堆,不粘了,不报错就没事
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# make
# 最后是这样的
make -C config all
make[1]: Entering directory `/home/data/soft/postgresql/postgresql-10.19/config'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/home/data/soft/postgresql/postgresql-10.19/config'
All of PostgreSQL successfully made. Ready to install.
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# make install
# 最后是这样的
make -C config install
make[1]: Entering directory `/home/data/soft/postgresql/postgresql-10.19/config'
/usr/bin/mkdir -p '/home/data/soft/postgresql/postgresql/lib/pgxs/config'
/usr/bin/install -c -m 755 ./install-sh '/home/data/soft/postgresql/postgresql/lib/pgxs/config/install-sh'
/usr/bin/install -c -m 755 ./missing '/home/data/soft/postgresql/postgresql/lib/pgxs/config/missing'
make[1]: Leaving directory `/home/data/soft/postgresql/postgresql-10.19/config'
PostgreSQL installation complete.

查看安装文件

[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# pwd
/home/data/soft/postgresql/postgresql-10.19
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# cd ../postgresql
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# ll
total 16
drwxr-xr-x 2 root root 4096 Dec 11 18:09 bin
drwxr-xr-x 6 root root 4096 Dec 11 18:09 include
drwxr-xr-x 4 root root 4096 Dec 11 18:09 lib
drwxr-xr-x 6 root root 4096 Dec 11 18:09 share

创建用户组和用户

[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# groupadd postgres
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# useradd -g postgres postgres
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# id postgres
uid=1001(postgres) gid=1002(postgres) groups=1002(postgres)

配置数据库

创建数据目录,赋予新用户权限

[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# pwd
/home/data/soft/postgresql/postgresql
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# mkdir data
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# chown postgres:postgres data
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# ls -al
total 28
drwxr-xr-x 7 root     root     4096 Dec 11 18:14 .
drwxr-xr-x 4 root     root     4096 Dec 11 17:54 ..
drwxr-xr-x 2 root     root     4096 Dec 11 18:09 bin
drwxr-xr-x 2 postgres postgres 4096 Dec 11 18:14 data
drwxr-xr-x 6 root     root     4096 Dec 11 18:09 include
drwxr-xr-x 4 root     root     4096 Dec 11 18:09 lib
drwxr-xr-x 6 root     root     4096 Dec 11 18:09 share

配置环境变量

[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# cd /home/postgres
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgres]# ls -al
total 20
drwx------  2 postgres postgres 4096 Dec 11 18:12 .
drwxr-xr-x. 5 root     root     4096 Dec 11 18:12 ..
-rw-r--r--  1 postgres postgres   18 Nov 25  2021 .bash_logout
-rw-r--r--  1 postgres postgres  193 Nov 25  2021 .bash_profile
-rw-r--r--  1 postgres postgres  231 Nov 25  2021 .bashrc
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgres]# vim .bash_profile
# 修改后的文件内容如下
# .bash_profile# Get the aliases and functions
if [ -f ~/.bashrc ]; then. ~/.bashrc
fi# User specific environment and startup programsexport PGHOME=/home/data/soft/postgresql/postgresql
export PGDATA=/home/data/soft/postgresql/postgresql/data/
# 遇到一个 Error while loading shared libraries: libpq.so.5 的错误,配置一下 lib 路径就好了
export LD_LIBRARY_PATH=/home/data/soft/postgresql/postgresql/libPATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/binexport PATH
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgres]# source .bash_profile

初始化数据库

[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ ~]$ initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/data/soft/postgresql/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Asia/Shanghai
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... okWARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.Success. You can now start the database server using:pg_ctl -D /home/data/soft/postgresql/postgresql/data/ -l logfile start
# 查看数据文件
[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ ~]$ cd /home/data/soft/postgresql/postgresql/data/
[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ data]$ ll
total 112
drwx------ 5 postgres postgres  4096 Dec 11 18:51 base
drwx------ 2 postgres postgres  4096 Dec 11 18:51 global
drwx------ 2 postgres postgres  4096 Dec 11 18:50 pg_commit_ts
drwx------ 2 postgres postgres  4096 Dec 11 18:50 pg_dynshmem
-rw------- 1 postgres postgres  4513 Dec 11 18:50 pg_hba.conf
-rw------- 1 postgres postgres  1636 Dec 11 18:50 pg_ident.conf
drwx------ 4 postgres postgres  4096 Dec 11 18:51 pg_logical
drwx------ 4 postgres postgres  4096 Dec 11 18:50 pg_multixact
drwx------ 2 postgres postgres  4096 Dec 11 18:50 pg_notify
drwx------ 2 postgres postgres  4096 Dec 11 18:50 pg_replslot
drwx------ 2 postgres postgres  4096 Dec 11 18:50 pg_serial
drwx------ 2 postgres postgres  4096 Dec 11 18:50 pg_snapshots
drwx------ 2 postgres postgres  4096 Dec 11 18:50 pg_stat
drwx------ 2 postgres postgres  4096 Dec 11 18:50 pg_stat_tmp
drwx------ 2 postgres postgres  4096 Dec 11 18:50 pg_subtrans
drwx------ 2 postgres postgres  4096 Dec 11 18:50 pg_tblspc
drwx------ 2 postgres postgres  4096 Dec 11 18:50 pg_twophase
-rw------- 1 postgres postgres     3 Dec 11 18:50 PG_VERSION
drwx------ 3 postgres postgres  4096 Dec 11 18:50 pg_wal
drwx------ 2 postgres postgres  4096 Dec 11 18:50 pg_xact
-rw------- 1 postgres postgres    88 Dec 11 18:50 postgresql.auto.conf
-rw------- 1 postgres postgres 23001 Dec 11 18:50 postgresql.conf

配置数据库监听信息

[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ data]$ vim postgresql.conf
## 修改内容如下:
listen_addresses = '*'          # what IP address(es) to listen on;
[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ data]$ vim pg_hba.conf
## 修改内容如下:
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all             all             0.0.0.0/0               trust

注册服务与开机自启

注册服务

[root@iZ0jlaqsl7s3tfa0rtwq5hZ bin]# cd /home/data/soft/postgresql/postgresql-10.19/contrib/start-scripts/
[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# ls
freebsd  linux  macos  osx
[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# chmod  a+x linux
[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# cp linux  /etc/init.d/postgresql

配置文件位置

[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# vim /etc/init.d/postgresql
# 修改内容如下
# Installation prefix 数据库文件安装位置
prefix=/home/data/soft/postgresql/postgresql# Data directory 数据库中数据存放位置
PGDATA="/home/data/soft/postgresql/postgresql/data"

开机自启

[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# chkconfig --add postgresql
[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# chkconfigNote: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd configuration.If you want to list systemd services use 'systemctl list-unit-files'.To see services enabled on particular target use'systemctl list-dependencies [target]'.netconsole         0:off 1:off 2:off 3:off 4:off 5:off 6:off
network         0:off 1:off 2:on  3:on  4:on  5:on  6:off
postgresql      0:off 1:off 2:on  3:on  4:on  5:on  6:off
redis_6379      0:off 1:off 2:on  3:on  4:on  5:on  6:off

重启数据库

[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# service postgresql start
Starting PostgreSQL: ok
[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]#

创建数据库信息

[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ ~]$ psql
psql (10.19)
Type "help" for help.
# 查看当前数据库
postgres=# \lList of databasesName    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +|          |          |             |             | postgres=CTc/postgrestemplate1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +|          |          |             |             | postgres=CTc/postgres
(3 rows)
# 创建新数据库
postgres=# CREATE DATABASE NAMIS;
CREATE DATABASE
# 创建用户并配置密码
postgres=# CREATE USER namis WITH PASSWORD 'Hywd@1qaz';
CREATE ROLE
# 赋予用户角色
postgres=# ALTER ROLE namis SUPERUSER;
ALTER ROLE
postgres=# \q
# 使用新用户登录
[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ ~]$ psql namis -U namis
psql (10.19)
Type "help" for help.namis=# \lList of databasesName    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------namis     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +|          |          |             |             | postgres=CTc/postgrestemplate1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +|          |          |             |             | postgres=CTc/postgres
(4 rows)namis=# \q

测试链接

开放端口

我这里使用的是本地 wsl,默认端口和主机相同,所以不需要开放端口,其他环境请酌情处理

错误提示

数据库无法启动

PostgreSQL 数据的data 文件夹不允许全组都有所有权限,我这里就是直接放权导致数据库无法启动

现象如下

# 首先使用 service postgresql start 命令启动,显示启动成功但是无法链接,也查不到数据库进程
# 后使用命令行的方式手动启动,报错如下
[postgres@LAPTOP-19HO0GA3 ~]$ pg_ctl start -D /home/soft/postgresql/postgresql/data/
waiting for server to start....2022-12-12 13:53:56.754 CST [320] FATAL:  data directory "/home/soft/postgresql/postgresql/data" has group or world access
2022-12-12 13:53:56.754 CST [320] DETAIL:  Permissions should be u=rwx (0700).stopped waiting
pg_ctl: could not start server
Examine the log output.

解决方案

# 重新为data 文件夹赋权
chmod -R 0700 data

PostgreSQL 数据库安装手册相关推荐

  1. PostgreSQL数据库安装Version10.5

    PostgreSQL数据库安装,基于版本10.5安装, 在Linux系统上使用*.gz二进制压缩包手动安装. 操作系统:Red Hat Enterprise Linux Server release ...

  2. PostgreSQL(Pgsql)快速开始/ ubuntu、centos下PostgreSQL数据库安装配置/查看已经编译的pg 编译选项

    文章目录 一.PostgreSQL是什么 1. Pgsql和Mysql的对比 二.ubuntu 下安装配置postgres sql 数据库 1. [推荐]apt-get 安装postgres 安装和配 ...

  3. linux下postgresql服务,Linux下PostgreSQL数据库安装、配置与日常服务管理

    1. 数据库安装与配置步骤 安装环境准备 操作系统: Oracle Linux Server 6.5 IP 地址: 192.168.6.177 版本信息: MySQL Community Server ...

  4. postgresql数据库安装及简单操作

    自从MySQL被Oracle收购以后,PostgreSQL逐渐成为开源关系型数据库的首选. 本文介绍PostgreSQL的安装和基本用法,供初次使用者上手.以下内容基于Debian操作系统,其他操作系 ...

  5. RedHat搭建Web运用之PostgreSQL数据库安装(一)

    开心一笑 [男:问你个问题,你喜欢猫还是狗? 女:狗狗. 男:汪汪汪! 女:-- 男:我太幸福了,你竟然喜欢我!] 视频教程 大家好,我录制的视频<Java之优雅编程之道>已经在CSDN学 ...

  6. PostgreSQL数据库安装教程

    关于PostgresSQL PostgresSQL数据库(简称Postgres或)正逐渐成为全球最流行的数据库之一,在DB-Engines官网发布的最新的全球最流行数据库排行榜中,PostgresSQ ...

  7. Linux系统DB2数据库安装手册

    1. 下载网址 http://happyqing.iteye.com/blog/2082305 2. 安装DB2 2.1 下载解压安装包 wget ftp://public.dhe.ibm.com/s ...

  8. postgresql数据库安装以及使用pgadmin连接postgresql数据库

  9. pg安装部署linux_Linux下postgresql数据库部署与配置

    1.检查postgresql是否已经安装:rpm -qa | grep postgres 2.检查PostgreSQL 安装位置:rpm -qal | grep postgres 3.卸载Postgr ...

最新文章

  1. opencv reduce函数
  2. c# 调matlab传字符串_C# Matlab 相互调用
  3. 《一只孤独的船》莱蒙托夫
  4. 僵尸进程孤儿进程与守护进程
  5. mac 下修改 jenkins 端口以及Jenkins的启动、关闭与更新
  6. 华为买买买!5000万美元收购一家安防技术公司 看中的是这个!
  7. 变通实现微服务的per request以提高IO效率(三)
  8. 瑟瑟发抖小网工之ISIS协议
  9. PDF如何提取其中一页,PDF提取页面的方法
  10. linux 磁盘碎片整理,Linux上没有磁盘碎片清理功能如何整理磁盘碎片
  11. 虚拟磁盘管理 无法启动服务器,运行虚拟磁盘管理器提示"RPC服务器不可用"分析与解决方案...
  12. css 多行文字左对齐,纯CSS实现文字一行居中,多行左对齐的方法
  13. 如何写一封稍微像样的求职邮件
  14. rx6600xt显卡相当于什么显卡
  15. 一个0中间一斜杠_斜杠青年喜欢的“网红台灯”:会唱歌,能无线充电,抖音获赞16W+...
  16. 【频率计】基于FPGA的频率计设计
  17. 正弦分析2--三角函数线
  18. python爬取淘宝商品做数据挖掘
  19. 中国大学MOOC·Python网络爬虫与信息提取(一)
  20. 2. STM32F4 USB协议研究 - SD卡模拟U盘

热门文章

  1. 简单记录下电脑Ubuntu系统卡死后无法正常开机的解救方式
  2. 绝地求生泰服显示服务器繁忙怎么解决,绝地求生泰服怎么进_泰服进不去_3DM网游...
  3. 蚂蚁学堂(1):10-Session与Cokile实现原理
  4. Docker by Lucien (一)
  5. ubuntu-14.10 输入法切换设置
  6. jq 移动端网页分享功能_jQuery实现移动端手机商城购物车功能
  7. 项目放到iis服务器,在Visual studio上发布web项目,并添加到IIS服务器上。
  8. 苹果手机人脸识别不了是什么原因_安卓手机越用越卡顿,为何苹果手机不会?离不开这3点原因...
  9. notepadd++ 实用的快捷键
  10. WebDAV之葫芦儿·派盘+网盘精灵