Linux系统PostgreSQL源码编译安装笔记【CentOS & Ubuntu篇】

操作系统:
    CentOS 7
    Ubuntu 18.04
安装版本:
    PostgreSQL 12.3 (当前最新版)

内容提要:

    1.在CentOS和Ubuntu两种主流Linux分支上源码编译安装PostgreSQL

    2.添加postgresql服务到Linux系统service以开机自启动(/etc/init.d)

    3.设置PostgreSQL默认超级账户`postgres'密码并开启远程访问

1.安装依赖

CentOS系统

yum install bison-devel readline-devel zlib-devel openssl-devel wget
yum groupinstall 'Development Tools'

Ubuntu系统

apt install build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt-dev libssl-dev libxml2-utils xsltproc

2.下载源码

wget https://ftp.postgresql.org/pub/source/v12.3/postgresql-12.3.tar.gz
tar -xz -f postgresql-12.3.tar.gz
cd postgresql-12.3/ ; ls

3.编译安装

./configure --prefix=/usr/local/pgsql
make && make install
useradd postgres
mkdir /usr/local/pgsql/data
chown -R postgres:postgres /usr/local/pgsql/
echo -e '# PostgreSQL PATH\nexport PATH=/usr/local/pgsql/bin:$PATH\n' >> /etc/profile
source /etc/profile

4.设置开机启动

cp contrib/start-scripts/linux /etc/init.d/postgresql
chmod a+x /etc/init.d/postgresql

CentOS系统

ln -s /etc/init.d/postgresql /etc/rc.d/rc0.d/K02postgresql
ln -s /etc/init.d/postgresql /etc/rc.d/rc1.d/K02postgresql
ln -s /etc/init.d/postgresql /etc/rc.d/rc2.d/K02postgresql
ln -s /etc/init.d/postgresql /etc/rc.d/rc3.d/S98postgresql
ln -s /etc/init.d/postgresql /etc/rc.d/rc4.d/S98postgresql
ln -s /etc/init.d/postgresql /etc/rc.d/rc5.d/S98postgresql
chkconfig --add postgresql
chkconfig postgresql on
firewall-cmd --add-port=5432/tcp --permanent
systemctl restart firewalld.service

Ubuntu系统

ln -s /etc/init.d/postgresql /etc/rc0.d/K02postgresql
ln -s /etc/init.d/postgresql /etc/rc1.d/K02postgresql
ln -s /etc/init.d/postgresql /etc/rc2.d/K02postgresql
ln -s /etc/init.d/postgresql /etc/rc3.d/S98postgresql
ln -s /etc/init.d/postgresql /etc/rc4.d/S98postgresql
ln -s /etc/init.d/postgresql /etc/rc5.d/S98postgresql
update-rc.d postgresql defaults

5.启动安装服务

这一步开始操作系统切换到`postgres'用户。

su - postgres
initdb -D /usr/local/pgsql/data
pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/data/serverlog start

6.开启远程访问

sed -i "s/host    all             all             127.0.0.1\/32            trust/# host    all             all             127.0.0.1\/32            trust/" /usr/local/pgsql/data/pg_hba.conf
echo 'host    all             all             0.0.0.0/0               md5' >> /usr/local/pgsql/data/pg_hba.conf
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/" /usr/local/pgsql/data/postgresql.conf
pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/data/serverlog reload

到此,就可以登录PG了。直接在命令行输入`psql'即可以PostgreSQL默认超级账户`postgres'无密码本地localhost登录。登录进PG以后设置一个密码,即可用其他GUI工具以TCP/IP远程访问本次安装的PostgreSQL。

$ psql
psql (12.3)
Type "help" for help.

postgres=# ALTER USER postgres WITH PASSWORD '1024';
ALTER ROLE
postgres=# SELECT VERSION();
                                               version                                               
-----------------------------------------------------------------------------------------------------
 PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0, 64-bit
(1 row)

postgres=# \q

因为之前已经将postgresql添加到操作系统/etc/init.d,以后就可以以系统管理员熟悉的service管理PG。

service postgresql status
service postgresql stop
service postgresql start
service postgresql restart
service postgresql reload

也可以以其他操作系统账户,如root账户,来直接登录PG。

root@ubuntu:~# psql -Upostgres
psql (12.3)
Type "help" for help.

postgres=# \q
root@ubuntu:~# psql -Upostgres -dpostgres
psql (12.3)
Type "help" for help.

postgres=# \q

 

 

参考:

https://www.postgresql.org/ftp/source/

https://www.postgresql.org/docs/current/installation.html

https://wiki.postgresql.org/wiki/Compile_and_Install_from_source_code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值