银河麒麟V10使用编译安装postgresql/postgis完整部署
虚拟机:银河麒麟V10
posgresql版本:postgresql-13.1
下载postgresql最新版:http://www.postgresql.org/ftp/source/
二、使用make方法安装posgresql1、首先下载源码postgres:
1 | [root@localhost ~]# wget http://ftp.postgresql.org/pub/source/v13.1/postgresql-13.1.tar.bz2 # 用时21分钟 |
2、解压
1 | [root@localhost ~]# tar xjvf postgresql-13.1.tar.bz2 |
3、进入文件夹
1 | [root@localhost ~]# cd postgresql-13.1/ |
4、 创建安装路径,一般我们都是安装在/opt/文件夹下面
1 2 3 | 这里一定要用root用户去创建 [root@localhost postgresql-13.1]# sudo su [root@localhost postgresql-13.1]# mkdir -p /opt/pgsql-13.1<br>创建好后一定是root:root 用户:用户组 |
5、配置
1 | [root@localhost postgresql-13.1]# ./configure --prefix=/opt/pgsql-13.1 --without-readlineView Code |

6、编译
1 | [root@localhost postgresql-13.1]# make -j8 # 安装时间大约3分钟 |
出现successfully表示安装成功
7、安装
1 | [root@localhost postgresql-13.1]# make install |
出现:PostgreSQL installation complete.安装完成
三、创建用户postgres,用来启动postgres服务。
1、创建postgres用户
1 | [root@localhost postgresql-13.1]# sudo su #登录root用户,如果是ubuntu桌面版的话 su -i [root@localhost postgresql-13.1]# useradd postgres #添加用户 [root@localhost postgresql-13.1]# passwd postgres #添加密码 我的密码 "`1234qwert" |
2、用户创建好以后就可以设置刚才/opt/pgsql-13.1的用户属性了
1 2 3 4 | #首先创建一个文件夹,作为数据库的数据存储点 sudo su mkdir /opt/pgsql-13.1/data chown -R postgres /opt/pgsql-13.1 |
3、设置环境变量
1 2 | [root@localhost ~]# sudo su [root@localhost ~]# |
在末尾打开后增加如下信息:i-->插入功能
1 2 3 4 5 | PATH=$PATH:/opt/pgsql-13.1/bin export PATH LD_LIBRARY_PATH=/opt/pgsql-13.1/lib export LD_LIBRARY_PATH export PGDATA=/opt/pgsql-13.1/data |
esc-->:wq退出
4、添加后source一下
1 | [root@localhost ~]# source /etc/profile |
5、进入postgres
1 | [root@localhost ~]# su - postgres |
注意,这时候命令行只有一个$符号,我们只需要输入bash后就可以了
bash cd / ls -l
postgres --version # 查看postgres版本
[root@localhost ~]# sudo su [root@localhost ~]# vim /etc/profile #设置数据库存放数据路径的全局变量 export PGDATA=/home/kylin/postgres/data
1 | <em>#或者 initdb -D /home/kylin/postgres/data</em><em>#如果export了就不用加-D xxx了:</em>initdb /home/kylin/postgres/datainitdb <br>pg_ctl start <br>#启动后postgres默认有一个用户postgres超级用户,因为它的密码是随机的,所以我们无法通过密码在其它用户上登录,所以必须通过postgres用户来登录超级用户,然后再创建一个数据库用户和数据库<br>psql -h 127.0.0.1 |
如下登录进来了
1 2 3 4 5 6 | postgres@kylin-D2000:/opt/pgsql-13.1$ psql -h 127.0.0.1 psql (12.9 (Ubuntu 12.9-0kylin0.20.04.1k1), 服务器 13.1) 警告:psql 主版本12,服务器主版本为13. 一些psql功能可能无法正常使用. 输入 "help" 来获取帮助信息. postgres=# |
到这里以后既可以创建数据库用户和database了。
1 2 3 4 5 6 7 8 9 | 创建用户 postgres=# create user username with password '****' ; CREATE ROLE postgres=# //修改用户名 alter user name rename to new naem //修改密码 alter user name with password '123456' ; |
创建数据库
postgres=# create database dbtest owner username; -- 创建数据库指定所属者
CREATE DATABASE
postgres=#
将数据库得权限,全部赋给某个用户
postgres=# grant all on database dbtest to username; -- 将dbtest所有权限赋值给username
GRANT
postgres=#
创建好之后就可退出
postgres=#\q
前面这么多工作主要为接下来做准备,其他linux用户登录数据库
1 2 3 4 5 | psql -h 10.10.10.10 -U user -d postgres -p 5432 -h:数据库IP -U:登录用户 -d:登录的数据库 -p:登录端口 |
这样你就可以在Linux下用你常用的Linux去登录数据库了
1 2 3 4 5 6 7 | kylin@kylin-D2000:~$ psql -U shenyan -d shenyan_f -h 127.0.0.1 psql (12.9 (Ubuntu 12.9-0kylin0.20.04.1k1), 服务器 13.1) 警告:psql 主版本12,服务器主版本为13. 一些psql功能可能无法正常使用. 输入 "help" 来获取帮助信息. shenyan_f=> |
注意:
值得注意的是posgres数据库服务在启动的时候必须是Linux postgres用户,这是postgres数据库默认的,所以你不去创建这个用户,postgres服务自己也会去创建,他创建后密码是随机生成的,我们是无法知道的,所以也无法登录postgres,所以还是我们自己手动去创建。
如果我们用命令行安装了postgres后。默认postgres会启动,同时它自己会建立
如果你嫌麻烦直接采用docker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@localhost ~]# docker pull postgres # 载入镜像 [root@localhost ~]# docker run \ --name demo \ --privileged=true \ -e TZ= 'Asia/Shanghai' \ -e POSTGRES_USER=koji \ -e POSTGRES_DB=koji \ -e POSTGRES_PASSWORD=123456 \ -e PGDATA=/tmp/ \ -v /root/data:/tmp/ \ -p 5432:5432 \ -v /root/pem/psql-run:/docker-entrypoint-initdb.d \ -d \ postgres |
然后就可以登录数据库了,登录后创建自己的数据库用户和数据库就可以了。
因为我们已经映射了data,所以只要/root/data在,即使数据库docker删除了,下次启动后,数据还是有的。
2、编译安装postgresql v10.0
1、 安装软件准备基于此情况,我们首先需要准备相应的安装包内容,下载地址参照 postgresql v10.0下载地址:https://www.postgresql.org/ftp/source/v10.0/
postgresql源码编译时的 ./configure 需要此软件,安装方式。右键选择在终端打开
1 | [root@localhost ~]# yum install readline-devel |
最好安装下openssl-devel,因为安装timescaledb时会提示这个未安装(一般情况也可以不安装)
1 | [root@localhost ~]# yum install openssl-devel |
查看我的postgers版本:rpm -qa | grep postgres
2、安装前可以先进行用户以及用户组的配置,方便后面进行授权(通过编译安装也需要,后续步骤会体现)。用户配置
[root@localhost ~]# groupadd postgres # 新增用户组
[root@localhost ~]# useradd -g postgres postgres # 创建用户组内用户
[root@localhost ~]# passwd postgres # 修改用户密码 我的密码:·1234qwert
3、文件包编译
将下载好的文件放入文件目录下,这里我放在postgresql目录下
[root@localhost home]# mkdir postgresql #创建postgresql目录 [root@localhost home]# cd /home/postgresql #切换到postgresql目录下 [root@localhost home]# tar -zvxf postgresql-10.0.tar.gz #解压文件 [root@localhost postgresql]# cd postgresql-10.0 #切换到postgresql-10.0目录下 [root@localhost postgresql-10.0]# ./configure #执行文件
1 | [root@localhost postgresql-10.0]# make <br>[root@localhost postgresql-10.0]# make install |
3.3、 安装完成后进行数据库data配置
进行配置,先创建好相应的目录文件
1 2 | mkdir -p /usr/ local /pgsql/data # 创建数据存储目录 chown -R postgres:postgres /usr/ local /pgsql/data # 用户/用户组目录授权 |
初始化数据库
1 | su - postgres # 切换postgres用户 |
1 2 3 4 5 6 7 | initdb -D /usr/ local /pgsql/data pg_ctl -D /usr/ local /pgsql/data -l logfile start createdb postgis psql postgis 这里可以查看数据库安装好后的对应版本 select version(); |
接着需要设置相应的环境变量,方便程序读取到指定内容
# 编辑用户下的环境变量配置文件
1 | vi ./.bash_profile |
输入如下内容:
1 2 3 4 5 | LD_LIBRARY_PATH=/usr/ local /pgsql/lib export LD_LIBRARY_PATH PATH=/usr/ local /pgsql/bin:$PATH export PATH export PGDATA=/usr/ local /pgsql/data |
编辑相应的数据库配置文件,修改postgresql.conf与pg_hba.conf
1 2 3 4 5 6 7 8 9 10 | cd /usr/ local /pgsql/data# 查找listen_addresses在postgresql.conf文件中的位置并显示行号 cat postgresql.conf | grep -n listen_addresses # 编辑postgresql.conf文件,修改默认 'localhost' 为 '*' vi postgresql.conf # 编辑IPv4 local connections: 追加一行,如图所示 vi pg_hba.conf # 修改完成后可以进行服务的重启 pg_ctl restart 此处截图为pg_hba.conf追加配置 |
设置服务自动启动
chkconfig --list
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了