ubuntu18.4-postgres11.2源码安装及配置启动

源码包下载:

1.现在postgres数据库二进制源码包

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

ubuntu18.4-postgres11.2源码安装及配置启动

 

 

 

 

2.解压安装postgres数据库

tar -zxvf postgres-XXX.tar.gz

ubuntu18.4-postgres11.2源码安装及配置启动

cd postgresql-11.2/

./configure

make && make install

安装过程中问题处理

  • configure报错

ubuntu18.4-postgres11.2源码安装及配置启动

configure报错

原因是缺少readline包

安装包: sudo apt install libreadline-dev

3.验证安装是否成功,输入命令:

ls -l /usr/local/pgsql/

如果 bin include lib 和 share都出现,说明安装成功

ubuntu18.4-postgres11.2源码安装及配置启动

4.设置用户账户(已有postgres账户可忽略此步骤)

adduser postgres

passwd postgres

5.创建postgresql 数据目录,分别运行:

mkdir /usr/local/pgsql/data

ubuntu18.4-postgres11.2源码安装及配置启动

chown postgres:postgres /usr/local/pgsql/data

验证

ls -ld /usr/local/pgsql/data

会出现如下界面

ubuntu18.4-postgres11.2源码安装及配置启动

6.初始化postgresql数据目录(须切换至postgres用户)

su - postgres

/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

ubuntu18.4-postgres11.2源码安装及配置启动

/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

ubuntu18.4-postgres11.2源码安装及配置启动

7.配置远程连接

    注:默认的PostgreSQL数据目录是/usr/local/pgsql/data/xxx.conf,

修改配置修改访问配置文件pg_hba.conf和配置文件postgresql.conf

    用vim修改两者,权限不够就加sudo或在root账户下操作。

    具体修改:将 #listen_addresses = 'localhost' 前的#号去掉,然后将后面的localhost改为*,然后将 #port = 5432 前的#去掉,最后再将#password_encryption = on 前面的#号去掉,如下图所示,使用wq命令保存退出。

ubuntu18.4-postgres11.2源码安装及配置启动

 (提示:postgresql.conf中可以修改的参数很多,上图中第2个红框可以修改侦听端口,另外此文件内还可以修改缓存大小等多种参数)。

    再对pg_hba.conf内容进行配置,将上面红框内的ident改为md5,然后再在最下面加入 host all all 0.0.0.0/0 md5 ,如下图所示:

ubuntu18.4-postgres11.2源码安装及配置启动

  最终修改结果(请仔细对比)

ubuntu18.4-postgres11.2源码安装及配置启动

8.配置服务启动项信息

进入postgresql 的安装目录(即刚刚使用tar命令解压的目录)

cd postgresql-XXX

 cp contrib/start-scripts/linux /etc/init.d/pgsql

ubuntu18.4-postgres11.2源码安装及配置启动

添加执行权限

chmod +x /etc/init.d/pgsql

启动数据库

service pgsql start

ubuntu18.4-postgres11.2源码安装及配置启动

9.测试连接

ubuntu18.4-postgres11.2源码安装及配置启动

10.卸载PostgreSQL
#通过运行以下命令完全删除PostgreSQL(注意重要数据提前备份)

sudo apt-get purge 'postgresql-*'
sudo apt-get autoremove 'postgresql-*'