💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
> 以源码编译的方式安装postgresql > 环境系统:centos7.4 > pgsql版本:postgresql13.0(本章节的后续文章均以此版本为基础) 一、创建源码存放目录并下载源码: ``` [root@izwz91quxhnlkan8kjak5hz ~] mkdir /postgresql [root@izwz91quxhnlkan8kjak5hz ~] cd /postgresql [root@izwz91quxhnlkan8kjak5hz postgresql] wget https://ftp.postgresql.org/pub/source/v13.0/postgresql-13.0.tar.bz2 ``` 二、解压源码: ``` [root@izwz91quxhnlkan8kjak5hz postgresql] tar -jxvf postgresql-13.0.tar.bz2 ``` 三、创建PostgreSQL安装目录 ``` [root@izwz91quxhnlkan8kjak5hz postgresql]# mkdir /www/server/postgresql ``` 四、运行configure对编译安装环境进行检查 ``` // 切换到源码根目录 [root@izwz91quxhnlkan8kjak5hz postgresql]# cd postgresql-13.0 [root@izwz91quxhnlkan8kjak5hz postgresql-13.0]# ./configure --prefix=/www/server/postgresql --with-perl --with-python ``` * --prefix 指定安装目录 * --with-perl 加上该选项才能使用Perl语法的PL/Perl过程语言来编写自定义函数。 * --with-python 加上该选项才能使用Python语法的PL/Python过程语言来编写自定义函数 configure运行可能的报错: 1、命令执行结果如发现报错:configure:error:readline library not found,可通过`yum install readline-dev` 命令来解决,安装完毕后重新执行configure。 2、命令执行结果如发现报错:configure: error: could not determine flags for linking embedded Perl。可通过`yum install perl-ExtUtils-Embed` 命令来解决,安装完毕后重新执行configure。<br> 五、编译安装 ``` // 执行make命令进行编译 [root@izwz91quxhnlkan8kjak5hz postgresql-13.0] make // 安装,最后显示PostgreSQL installation complete 即可 [root@izwz91quxhnlkan8kjak5hz postgresql-13.0] make install ... make[1]: 进入目录“/postgresql/postgresql-13.0/config” /usr/bin/mkdir -p '/www/server/postgresql/lib/pgxs/config' /usr/bin/install -c -m 755 ./install-sh '/www/server/postgresql/lib/pgxs/config/install-sh' /usr/bin/install -c -m 755 ./missing '/www/server/postgresql/lib/pgxs/config/missing' make[1]: 离开目录“/postgresql/postgresql-13.0/config” PostgreSQL installation complete. [root@izwz91quxhnlkan8kjak5hz postgresql-13.0]# ``` 六、添加系统用户(postgres)并创建pgsql数据存储位置 ``` [root@izwz91quxhnlkan8kjak5hz postgresql-13.0]# useradd postgres [root@izwz91quxhnlkan8kjak5hz postgresql-13.0]# passwd postgres [root@izwz91quxhnlkan8kjak5hz postgresql-13.0]# mkdir /www/server/data/postgres [root@izwz91quxhnlkan8kjak5hz postgresql-13.0]# chown -R postgres:postgres /www/server/data/postgres ``` 七、配置postgres用户的环境变量 ``` // 切换用户登录 [postgres@izwz91quxhnlkan8kjak5hz postgresql-13.0]$ su - postgres [postgres@izwz91quxhnlkan8kjak5hz ~]$ whoami postgres // 编辑~/.bashrc文件,在文件末尾新增以下环境变量 [postgres@izwz91quxhnlkan8kjak5hz ~]$ vim ./.bashrc export PGHOME=/www/server/postgresql export PGDATA=/www/server/data/postgres export PATH=$PGHOME/bin:$PATH export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH export MANPATH=$PGHOME/share/man:$MANPATH // 重启让配置生效 [postgres@izwz91quxhnlkan8kjak5hz ~]$ source ~/.bashrc ``` 八、初始化数据库 ``` [postgres@izwz91quxhnlkan8kjak5hz ~] cd /www/server/postgresql/bin [postgres@izwz91quxhnlkan8kjak5hz bin] initdb ``` ![](https://img.kancloud.cn/82/c3/82c3b2ce084ae6b67e1fc81a61befda7_667x637.png) 九、创建数据库运行日志文件并启动数据库 ``` //需切换为root用户 [root@izwz91quxhnlkan8kjak5hz ~]# mkdir -p /var/log/postgres [root@izwz91quxhnlkan8kjak5hz ~]# cd /var/log/postgres/ [root@izwz91quxhnlkan8kjak5hz postgres]# touch pg_server.log [root@izwz91quxhnlkan8kjak5hz postgres]# chmod -R 755 pg_server.log [root@izwz91quxhnlkan8kjak5hz postgres]# chown -R postgres:postgres /var/log/postgres/pg_server.log [root@izwz91quxhnlkan8kjak5hz postgres]# // 创建完毕后切换回postgres用户 [postgres@izwz91quxhnlkan8kjak5hz postgres]# cd /www/server/postgresql/bin [postgres@izwz91quxhnlkan8kjak5hz bin]# pg_ctl -D /www/server/data/postgres -l /var/log/postgres/pg_server.log start waiting for server to start.... done server started ``` 十、测试:执行psql来登录数据库(postgres用户) ``` // 使用 psql 命令行工具连接数据库 [postgres@izwz91quxhnlkan8kjak5hz bin]$ psql psql (13.0) Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows) postgres=# ```