环境:Centos7.4

PostgreSQL-11.7可以在官网下载

一、创建postgres用户 (root)
useradd postgres
echo 'postgres:passwd' | sudo chpasswd
echo 'postgres ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
mkdir -p /app/postgres
chown postgres.postgres  /app/postgres
chmod -R 775 /app

二、yum依赖包(root用户)
yum -y install unzip zip lrzsz vim readline readline-devel openssl openssl-devel zlib zlib-devel

三、查询关闭防火墙(root用户)

firewall-cmd --state
systemctl stop firewalld.service
systemctl disable firewalld.service
------------------------------------------------------------
或开通PostgreSQL端口:
firewall-cmd --zone=public --add-port=5432/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-ports
------------------------------------------------------------

四、修改主机名称(root用户)
hostnamectl --static set-hostname test-postgres-001

五、检查安装环境 (root)
su postgres
mkdir postgresql
mkdir -p /app/postgres/postgresql/data
mkdir -p /app/postgres/postgresql/log
#切换root
sudo su - 
# 进入解压后的源码文件夹(/app/postgresql-11.7)
cd /app/postgresql-11.7
# 检查、并配置(--prefix=/app/postgres/postgresql 为配置postgreSQL的安装路径)
./configure --prefix=/app/postgres/postgresql
------------------------------------------------------------------------------------
configure: creating ./config.status
config.status: creating GNUmakefile
config.status: creating src/Makefile.global
config.status: creating src/include/pg_config.h
config.status: creating src/include/pg_config_ext.h
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s
config.status: linking src/backend/port/dynloader/linux.c to src/backend/port/dynloader.c
config.status: linking src/backend/port/posix_sema.c to src/backend/port/pg_sema.c
config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
config.status: linking src/backend/port/dynloader/linux.h to src/include/dynloader.h
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port

------------------------------------------------------------------------------------
六、编译及安装(root 时间较长)
make && make install
------------------------------------------------------------------------------------
make -C config install
make[1]: Entering directory `/app/postgresql-11.7/config'
/bin/mkdir -p '/app/postgres/postgresql/lib/pgxs/config'
/bin/install -c -m 755 ./install-sh '/app/postgres/postgresql/lib/pgxs/config/install-sh'
/bin/install -c -m 755 ./missing '/app/postgres/postgresql/lib/pgxs/config/missing'
make[1]: Leaving directory `/app/postgresql-11.7/config'
PostgreSQL installation complete.
[root@localhost postgresql-11.7]#

------------------------------------------------------------------------------------
#使用root授权
sudo chown -R postgres:postgres /app/postgres/postgresql

六、配置环境变量 (postgres)
su - postgres
切换到用户根目录下(/home/postgres)
cd ~
编辑 .bash_profile
vi ~/.bash_profile

export PGHOME=/app/postgres/postgresql
export PGDATA=/app/postgres/postgresql/data            
export PGLOG=/app/postgres/postgresql/log/serverlog
export PATH=$PGHOME/bin:$PATH

source /etc/profile

七、初始化数据库(postgres)
使用postgres用户执行如下命令:
cd /app/postgres/postgresql/
initdb -D /app/postgres/postgresql/data -U postgres --locale=en_US.UTF8 -E UTF8

看到如下画面表示数据库初始化成功:
初始化postgreSQL (/app/postgres/postgresql)
#/app/postgres/postgresql/data数据文件的存放路径
------------------------------------------------------------------------------------
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: 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 /app/postgres/postgresql/data -l logfile start

[postgres@paas-test-postgres-001 bin]$ pwd
初始完数据库之后,data文件夹会出现一系列文件
drwx------. 5 postgres postgres    41 Mar 17 00:31 base
drwx------. 2 postgres postgres  4096 Mar 17 00:31 global
drwx------. 2 postgres postgres     6 Mar 17 00:31 pg_commit_ts
drwx------. 2 postgres postgres     6 Mar 17 00:31 pg_dynshmem
-rw-------. 1 postgres postgres  4513 Mar 17 00:31 pg_hba.conf
-rw-------. 1 postgres postgres  1636 Mar 17 00:31 pg_ident.conf
drwx------. 4 postgres postgres    68 Mar 17 00:31 pg_logical
drwx------. 4 postgres postgres    36 Mar 17 00:31 pg_multixact
drwx------. 2 postgres postgres    18 Mar 17 00:31 pg_notify
drwx------. 2 postgres postgres     6 Mar 17 00:31 pg_replslot
drwx------. 2 postgres postgres     6 Mar 17 00:31 pg_serial
drwx------. 2 postgres postgres     6 Mar 17 00:31 pg_snapshots
drwx------. 2 postgres postgres     6 Mar 17 00:31 pg_stat
drwx------. 2 postgres postgres     6 Mar 17 00:31 pg_stat_tmp
drwx------. 2 postgres postgres    18 Mar 17 00:31 pg_subtrans
drwx------. 2 postgres postgres     6 Mar 17 00:31 pg_tblspc
drwx------. 2 postgres postgres     6 Mar 17 00:31 pg_twophase
-rw-------. 1 postgres postgres     3 Mar 17 00:31 PG_VERSION
drwx------. 3 postgres postgres    60 Mar 17 00:31 pg_wal
drwx------. 2 postgres postgres    18 Mar 17 00:31 pg_xact
-rw-------. 1 postgres postgres    88 Mar 17 00:31 postgresql.auto.conf
-rw-------. 1 postgres postgres 23986 Mar 17 00:31 postgresql.conf

1、base目录是表空间目录;
2、global目录是相关全局变量目录;
3、pg_hba.conf是访问控制配置文件;
4、postgresql.conf是postgresql的主配置文件。
------------------------------------------------------------------------------------

八、设置参数文件(postgres)
1)修改pg_hba.conf文件
   使用postgres用户,执行vi /app/postgres/postgresql/data/pg_hba.conff 修改如下参数
   将该文件中的IPV4的连接修改为0.0.0.0/0,表示信任来自所有id连接的客户端,加密方式改为md5
    host    all             all             0.0.0.0/0            md5
    -------------------------------------------------------------------------------------------------------------------
    METHOD指定如何处理客户端的认证。常用的有ident,md5,password,trust,reject
    ident是Linux下PostgreSQL默认的local认证方式,凡是能正确登录服务器的操作系统用户(注:不是数据库用户)就能使用本用户映射的数据库用户不需密码登录数据库。用户映射文件为pg_ident.conf,这个文件记录着与操作系统用户匹配的数据库用户,如果某操作系统用户在本文件中没有映射用户,则默认的映射数据库用户与操作系统用户同名。比如,服务器上有名为user1的操作系统用户,同时数据库上也有同名的数据库用户,user1登录操作系统后可以直接输入psql,以user1数据库用户身份登录数据库且不需密码。很多初学者都会遇到psql -U username登录数据库却出现“username ident 认证失败”的错误,明明数据库用户已经createuser。原因就在于此,使用了ident认证方式,却没有同名的操作系统用户或没有相应的映射用户。解决方案:1、在pg_ident.conf中添加映射用户;2、改变认证方式。
    md5是常用的密码认证方式,如果你不使用ident,最好使用md5。密码是以md5形式传送给数据库,较安全,且不需建立同名的操作系统用户。
    password是以明文密码传送给数据库,建议不要在生产环境中使用。
    trust是只要知道数据库用户名就不需要密码或ident就能登录,建议不要在生产环境中使用。
    reject是拒绝认证。
    在文件查找 listen_addresses,他的值说明
    如果希望只能从本地计算机访问PostgreSQL数据库,就将该项设置为'localhost';
    如果希望从局域网访问PostgreSQL数据库,就将该项设置为PostgreSQL数据库的局域网IP地址;
    如果希望从互联网访问PostgreSQL数据库,就将该项设置为PostgreSQL数据库的互联网IP地址;
    如果希望从任何地方都可以访问PostgreSQL数据库,就将该配置项设置为“*”;
    -------------------------------------------------------------------------------------------------------------------
    
2)使用postgres用户,执行vi /app/postgres/postgresql/data/postgresql.conf 修改如下参数(监听地址、监听端口、最大连接数):
将localhost改为*,放开listen_addresses = '*',表示监听所有的网络连接。其他的参数保持默认即可 port = 5432 默认端口
listen_addresses = '*'
port = 5432
max_connections = 100

九、启动数据库(postgres)
pg_ctl -D /app/postgres/postgresql/data -l logfile start

其中-l代表日志文件目录,现在是当前目录,但是最好是新建一个log文件夹 指定pg_ctl -D /app/postgres/postgresql/data -l /app/postgres/postgresql/log start
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[postgres@paas-test-postgres-001 bin]$ pg_ctl -D /app/postgres/postgresql/data -l logfile start
waiting for server to start.... done
server started
[postgres@paas-test-postgres-001 bin]$ pwd
/app/postgres/postgresql/bin
--------------------------------------------------------
于是postgresql数据库启动了!我们首先可以通过postgresql客户端psql来进行连接,直接输入psql既可以进入:
使用psql客户端
postgres=# SELECT pg_reload_conf();
使用pg_ctl命令(PG内置命令)
pg_ctl reload
----------------------------------------------------------------------------------------------------------------------------

第一次登录
现在就是需要进行设置postgres用户的密码(默认为空),用psql连接成功后直接输入\password即提示输入两次密码,或者在psql命令提示符下输入如下命令:alter user postgres with password 'postgres';
开启远程登陆之后,要想在服务器控制台登陆只能这样psql -h 你服务器Ip -U postgres

[postgres@paas-test-postgres-001 bin]$ psql
psql (11.7)
Type "help" for help.

postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF8 | en_US.UTF8 | 
 template0 | postgres | UTF8     | en_US.UTF8 | en_US.UTF8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF8 | en_US.UTF8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(3 rows)

postgres=# \password
Enter new password: 
Enter it again: 
postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF8 | en_US.UTF8 | 
 template0 | postgres | UTF8     | en_US.UTF8 | en_US.UTF8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF8 | en_US.UTF8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(3 rows)
postgres=#

----------------------------------------------------------------------------------------------------------------------------
postgres=# quit 退出

十、停止数据库(postgres)
pg_ctl stop -D $PGDATA -m fast -W

pg_ctl stop -D /app/postgres/postgresql/data -m fast -W
---
[postgres@paas-test-postgres-001 bin]$ pg_ctl stop -D /app/postgres/postgresql/data -m fast -W
server shutting down
[postgres@paas-test-postgres-001 bin]$ 
---
启动
# /home/postgres/PGDATA为数据文件存放路径,里面也有一些配置文件
pg_ctl -D /app/postgres/postgresql/data -l logfile start
---
[postgres@paas-test-postgres-001 bin]$ pg_ctl -D /app/postgres/postgresql/data -l logfile start
waiting for server to start.... done
server started
[postgres@paas-test-postgres-001 bin]$ 
---

停止
pg_ctl -D /app/postgres/postgresql/data -l logfile stop
---
[postgres@paas-test-postgres-001 bin]$ pg_ctl -D /app/postgres/postgresql/data -l logfile stop
waiting for server to shut down.... done
server stopped
[postgres@paas-test-postgres-001 bin]$ 
---

重启
pg_ctl -D /app/postgres/postgresql/data -l logfile restart
---
[postgres@paas-test-postgres-001 bin]$ pg_ctl -D /app/postgres/postgresql/data -l logfile restart
waiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started
[postgres@paas-test-postgres-001 bin]$

---

查看状态
pg_ctl -D /app/postgres/postgresql/data -l logfile status
---
[postgres@paas-test-postgres-001 bin]$ pg_ctl -D /app/postgres/postgresql/data -l logfile status
pg_ctl: server is running (PID: 38326)
/app/postgres/postgresql/bin/postgres "-D" "/app/postgres/postgresql/data"
---

11、配置系统服务,开机自启动(root)
进入postgresql源码包的解压目录(本文为/root/postgresql-11.7),执行命令 cp contrib/start-scripts/linux /etc/init.d/postgresql,然后 vim /etc/init.d/postgresql,进行如下配置修改
-----
# contrib/start-scripts/linux

## EDIT FROM HERE
# Installation prefix
prefix=/app/postgres/postgresql

# Data directory
PGDATA="/app/postgres/postgresql/data"

# Who to run the postmaster as, usually "postgres".  (NOT"root")
PGUSER=postgres

# Where to keep a log file
PGLOG="$prefix/log/serverlog"
-----
然后再分别执行如下命令:
chmod u+x /etc/init.d/postgresql
chkconfig --add postgresql
chkconfig --list postgresql
---------------

远程连接
远程连接这里使用的是Navicat for PostgreSQL
--注意这一段:tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      38898/postgres
------------------------------------------------------------------------------------------------------------------------
ps -ef | grep -i post
[postgres@paas-test-postgres-001 bin]$ netstat -anlp | grep  5432
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      38898/postgres      
tcp        0      0 192.168.3.21:5432       192.168.3.1:60644       ESTABLISHED 38908/postgres: pos 
tcp6       0      0 :::5432                 :::*                    LISTEN      38898/postgres      
unix  2      [ ACC ]     STREAM     LISTENING     60774    38898/postgres       /tmp/.s.PGSQL.5432

------------------------------------------------------------------------------------------------------------------------

基本操作:
创建db、创建用户、分配权限

CREATE DATABASE testdb;

CREATE USER testuser CREATEDB LOGIN PASSWORD 'dbpasswd';

GRANT ALL ON DATABASE testdb TO testuser;

#删除db、删除用户
drop database testdb;
drop role testuser;

---------------------------------------------
其他相关命令
在数据库命令行执行
\l 查看所有数据库
\c db 来进入db数据库
\d 查看数据库表
创建数据库database
CREATE DATABASE Tdatabase;
创建用户auser
CREATE USER auser CREATEDB LOGIN PASSWORD 'Upasswd';
将Tdatabase所有权限赋给用户auser
GRANT ALL ON DATABASE Tdatabase TO auser;
grant all privileges on database 数据库名称 to dbuser;
删除数据库Tdatabase
drop database database;
删除用户auser
drop role auser;
插入数据
insert into 表名 values(..,..);
创建表
create table "数据库名"."表名" (
.....
);
----------------------------------------

PostgreSQL-11.7关系型数据库源码安装相关推荐

  1. mysql数据库源码安装_学习笔记-源码安装mariadb 20210128

    源码安装Mariadb数据库 安装之前先检查一下空间: 1 [15:13:16 root@centos8 ~]#free -h(#检查空间)2 total used free shared buff/ ...

  2. PostgreSQL数据库源码安装第一步——configure脚本分析(环境检测)

    源码编译PG8.4.1时,需要在安装完几个依赖的开发库后,在源代码目录下运行configure脚本.下面我们就分析一下该名为configure的shell脚本.其主要功能是猜测系统依赖变量的值并创建M ...

  3. linux安装pgsql源码包解压,在Linux(centos)中使用源码安装pgRouting

    目录 一.解压pgRouting 二.配置PostgreSQL的环境变量 三.编译源代码 四.测试 由于pgRouting依赖于PostGIS和PostgreSQL,所以阅读本文之前,务必请先阅读: ...

  4. mysql5.6.35源码安装记录

    mysql数据库源码安装: 源码地址:wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.xx.tar.gz #安装前准备, ...

  5. Linux安装MySQL(源码安装)

    文章目录 一.下载 二.最小化安装配置 三.MySQL的安装的几种方式 1.MySQL安装方式 2. 三种安装方式的区别 四.MySQL的GLIBC版本安装 1. 上传软件包解压 2. 软件安装 3. ...

  6. Centos 7环境下源码安装PostgreSQL数据库

    马上就要去实习了,工作内容是搞数据仓库方面的,用的是postgresql关系型数据库,于是自己先来了解下这种数据的用法,之后说说这个数据库和MySQL的关系和区别. 1.Postgresql简介 看了 ...

  7. centos7源码安装mysql报错_CentOS7 下源码安装MySQL数据库 8.0.11

    本文主要向大家介绍了CentOS7 下源码安装MySQL数据库 8.0.11,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. CentOS7 下源码安装MySQL 8.0.11 系 ...

  8. 源码安装Bind 9.10 正式版 开启DLZ数据库支持 和 数据库view查询

    昨天看见新闻,说Bind 9.10.3版本已经正式发布了,迫不及待安装试试,,, 我前面的文章已经体验过 bind 9.10的RC版的个别新功能, 见文 Bind 9.10 源码安装 以及 新增red ...

  9. PostGreSQL(1)-源码安装

    目录 简述 一.格式化磁盘 二.源码安装 PostGreSql 1. 安装 readline-devel 2. 安装 PostGresql 3. 设置环境变量 三. 初始化 1. 设置运行用户 2. ...

  10. mysql 5.5.38_数据库的硬迁移和mysql 5.5.38源码安装

    场景: 一台服务器出现了故障,需要把数据库迁移到另一台服务器上,由于数据效大,直接压缩数库 的数据目录(在数据库DOWN的情况下) 数据库版本mysql 5.5.38 安装方式:源码安装 思路:在另一 ...

最新文章

  1. 一文详解CMakeLists文件编写语法规则详解
  2. Btrace详细指南(JDK7,监控HashMap扩容)
  3. 匈牙利算法c语言代码,漫谈匈牙利算法
  4. ITK:写一个TIFF图像
  5. 为特使构建控制平面的指南-为可插入性构建
  6. 用计算机打出二分之一,win10手机计算器怎么输入二分之一?
  7. 以太坊同步节点_如何同步以太坊节点而不会犯我的错误
  8. mysql 数据库编码怎么看,查看数据库编码_查看mysql编码方式 mysql数据库编码查看方法...
  9. mysql安装设置mysql字符集utf8及修改密码
  10. chrome里面模拟手机上打开网页的场景方法
  11. C中的预编译宏定义-转
  12. python零基础好学吗-Python零基础好学吗?零基础如何学习Python?
  13. 力扣题:904.水果成篮
  14. 什么是交换机?跟服务器之间有什么联系吗?
  15. ESP32彩屏显示入门(一):我要五彩斑斓的黑,还有五光十色的白 | ESP32轻松学(Arduino版)
  16. 今日早报 每日精选12条新闻简报 每天一分钟 知晓天下事 6月29日
  17. 【Linux 内核 内存管理】内存映射相关数据结构 ③ ( vm_area_struct 结构体成员分析 | shared 成员 | anon_vma_chain 成员 | anon_vma 成员 )
  18. 罗马数字转换python_20190502-罗马数字转换为数字
  19. vue使用element-ui table 清除表格背景色以及表格边框线
  20. Unity3d架构之-Unity MVC框架 StrangeIoC

热门文章

  1. proposal中文翻译_proposal是什么意思_ proposal的翻译_音标_读音_用法_例句_爱词霸在线词典...
  2. Word在试图打开文件时错误,如何解决
  3. Flutter 闪屏页实现
  4. 高等代数--线性变换
  5. 如何把.bat文件设置为开机自动启动?
  6. 第十一个Java程序,计算QQ等级。
  7. 【读书】兔兔西的已读书单
  8. maven的pom文件引入依赖报错:提示无法处理 Cannot resolve...
  9. Font Replacer
  10. 局域网即时通讯软件的实现