Pg12/Centos7 源码编译安装部署文档

========================== 环境准备 ==========================

1、下载安装包到/opt目录:
wget https://ftp.postgresql.org/pub/source/v12.1/postgresql-12.1.tar.gz

2、解压:tar -xvf postgresql-12.1.tar.gz

3、安装依赖包:
yum groupinstall "Development tools"
yum install -y bison flex readline-devel zlib-devel

========================== 编译安装 ==========================

4、设置软连接,个人习惯放到/usr/local目录下:
[root@otter3 opt]# cd /usr/local/
[root@otter3 local]# ln -s /opt/postgresql-12.1 pg12

5、进入目录编译安装:
[root@otter3 local]# cd /usr/local/pg12
[root@otter3 pg12]# ./configure --prefix=/usr/local/pg12/ --with-pgport=1921

如果报错:configure: error: no acceptable C compiler found in $PATH
安装:yum install gcc可以解决

执行编译:gmake world -- 如果看到PostgreSQL, contrib, and documentation successfully made. Ready to install 说明成功
执行安装:gmake install-world -- 如果看到PostgreSQL, contrib, and documentation installation complete 说明成功
注:如果报错软件包不存在yum安装即可

6、进入安装目录执行查看版本命令:
[root@otter3 pg12]# ./bin/postgres --version
postgres (PostgreSQL) 12.1

========================== DB初始化 ==========================

7、创建用户,用来管理pg
[root@otter3 bin]# groupadd -g 2000 postgres
[root@otter3 bin]# useradd -g 2000 -u 2000 postgres
[root@otter3 bin]# id postgres
uid=2000(postgres) gid=2000(postgres) groups=2000(postgres)

8、创建数据库目录(数据目录安装在data目录下,这些都是后期必用的文件夹):
[root@otter3 pgdata]# mkdir -p /data/pgdata/pg12/{data,backups,scripts,archive_wals}

9、目录赋权(后期即使用initdb工具初始化时会自动回收非postgres用户权限,但这里先手动赋权养成好习惯):
[root@otter3 pgdata]# chown -R postgres:postgres /data/pgdata/pg12
[root@otter3 pgdata]# chmod 0700 /data/pgdata/pg12

10、切换postgres用户并初始化数据库:
[root@otter3 bin]# su - postgres
[postgres@otter3 ~]$ /usr/local/pg12/bin/initdb -D /data/pgdata/pg12/data -W
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.
Enter new superuser password:
Enter it again:
fixing permissions on existing directory /data/pgdata/pg12/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: 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:
/usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data -l logfile start

注:因为初始化时指定了参数W,所以在initdb工具初始化时要求创建密码

11、根据提示启动数据库命令启动数据库:/usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data -l logfile start

12、查看数据库状态:
[postgres@otter3 ~]$ /usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data status
pg_ctl: server is running (PID: 30677)
/opt/postgresql-12.1/bin/postgres "-D" "/data/pgdata/pg12/data"

13、使用工具检测数据库是否接受连接:
[postgres@otter3 ~]$ /usr/local/pg12/bin/pg_isready -p 1921
/tmp:1921 - accepting connections

========================== 启动关闭 ==========================

14、启动方式1:[postgres@otter3 ~]$ /usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data start

启动方式2:[postgres@otter3 ~]$ /usr/local/pg12/bin/postgres -D /data/pgdata/pg12/data &

关闭方式1:[postgres@otter3 ~]$ /usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data stop

关闭方式2:kill -sigterm head -1 /data/pgdata/pg12/data/postmaster.pid

15、配置服务器脚本拷贝启动脚本并命名为pg12:
root用户下进入目录:
[postgres@otter3 ~]$ exit
logout
[root@otter3 pgdata]# cd /usr/local/pg12/contrib/start-scripts
[root@otter3 start-scripts]# cp linux /etc/init.d/pg12
赋权并查看:
[root@otter3 init.d]# chmod +x /etc/init.d/pg12
[root@otter3 init.d]# ls -lh /etc/init.d/pg12
设置开启启动/关闭:
[root@otter3 init.d]# chkconfig pg12 on -- 关闭 off
[root@otter3 init.d]# chkconfig --list | grep pg12

========================== 环境变量 ==========================
用postgres用户设置环境变量,编辑 .bash_profile 文件添加一下内容:
export PGDATA=/data/pgdata/pg12/data
export PATH=/usr/local/pg12/bin:$PATH
export LD_LIBRARY=/usr/local/pg12/lib:$LD_LIBRARY_PATH
export LANG=en_US.UTF-8

source .bash_profile
========================== 远程访问 ==========================

16、数据库配置基础:
重要配置文件(postgresql.conf:主要负责:文件位置、资源限制、复制集群等
pg_hba.conf:主要负责:客户端连接和认证-- 数据库的防火墙)

17、配置远程访问:
默认情况下pg不允许通过远程访问数据库:
[postgres@otter3 data]$ netstat -nlt | grep 1921
tcp 0 0 0.0.0.0:1921 0.0.0.0:* LISTEN
tcp6 0 0 :::1921 :::* LISTEN
通过以下方式可以修改:
[root@otter3 init.d]# vim /data/pgdata/pg12/data/postgresql.conf
1、修改监听地址为*
listen_addresses = '*' # what IP address(es) to listen on;
                                    # comma-separated list of addresses;
                                    # defaults to 'localhost'; use '*' for all
                                    # (change requires restart)

#port = 1921 # (change requires restart)

注意:修改配置文件需要重启(注意切换用户postgres),命令参考:/usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data restart
同时修改:pg_hba.conf
[root@otter3 init.d]# echo "host all all 0.0.0.0/0 md5" >> /data/pgdata/pg12/data/pg_hba.conf
[root@otter3 init.d]# /usr/local/pg12/bin/pg_ctl -D /data/pgdata/pg12/data reload

========================== OVER ==========================

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容