Ubuntu环境PostgreSQL源码编译安装

一、pg数据库安装包下载

下载地址:http://www.postgresql.org/ftp/source/
Ubuntu环境PostgreSQL源码编译安装_第1张图片
Ubuntu环境PostgreSQL源码编译安装_第2张图片

二、安装依赖包

sudo apt-cache search readline
sudo apt install lib64readline8 lib64readline-dev 
sudo apt install zlib1g-dev
sudo apt install gcc
sudo apt install  libreadline6-dev
sudo apt install make

三、安装postgres

进入/opt目录,新建两个包,其中software放压缩包,module放解压和安装后的软件

[root@localhost opt]# mkdir software
[root@localhost opt]# mkdir module

然后加入到software中,将下载下来的压缩包传上去

[root@localhost opt]# cd software

然后解压缩

[root@localhost software]# tar -zxvf postgresql-14.4.tar.gz 

然后将解压缩得到的postgresql-14.4移动到module中

[root@localhost software]# mv postgresql-14.4 /opt/module/

进入postgresql-14.4

[root@localhost software]# cd  /opt/module/postgresql-14.4

编译postgresql源码,报错

[root@localhost postgresql-14.4]# ./configure --prefix=/opt/module/pgsql

Ubuntu环境PostgreSQL源码编译安装_第3张图片
再先后执行make和make install

[root@localhost postgresql-14.4]# make
[root@localhost postgresql-14.4]# make install

四、添加用户

如果执行失败,添加sudo试下

useradd -m -d /home/postgres -s /bin/bash postgres

修改postgres用户的密码

passwd postgres

五、创建postgresql数据库的数据主目录并修改文件所有者

[root@localhost postgresql-14.4]# cd /opt/module/pgsql/
[root@localhost pgsql]# ls
bin  include  lib  share
[root@localhost pgsql]# mkdir data
[root@localhost pgsql]# chown postgres:postgres data
[root@localhost pgsql]# ls
bin  data  include  lib  share

六、配置环境变量

配置环境变量(注意,这里的路径要改为你的安装路径,即configure --prefix=PATH 中PATH的路径)

export PGHOME=/opt/module/pgsql
export PGDATA=/opt/module/pgsql/data
export PGPORT=5432
export PGUSER=postgres
export PATH=$PGHOME/bin:$PATH:$HOME/bin
export LD_LIBRARY_PATH=/opt/module/pgsql/lib

七、切换用户到postgres并使用initdb初使用化数据库

[root@localhost postgres]# su - postgres
Last login: Thu Aug  4 22:16:27 PDT 2022 on pts/0
[postgres@localhost ~]$ initdb
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.

fixing permissions on existing directory /opt/module/pgsql/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 ... America/Los_Angeles
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:

    pg_ctl -D /opt/module/pgsql/data -l logfile start

可以看到/opt/module/pgsql/data中已经有数据了

[postgres@localhost data]$ ls
base          pg_ident.conf  pg_serial     pg_tblspc    postgresql.auto.conf
global        pg_logical     pg_snapshots  pg_twophase  postgresql.conf
pg_commit_ts  pg_multixact   pg_stat       PG_VERSION   test.txt
pg_dynshmem   pg_notify      pg_stat_tmp   pg_wal
pg_hba.conf   pg_replslot    pg_subtrans   pg_xact

八、配置服务

修改/opt/module/pgsql/data目录下的两个文件。
postgresql.conf 配置PostgreSQL数据库服务器的相应的参数。
pg_hba.conf 配置对数据库的访问权限。

[postgres@localhost data]$ vim postgresql.conf 

将listen_addresses和port前的注释去掉,并修改listen_addresses的值为*

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '*'          # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
port = 5432                             # (change requires restart)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
#unix_socket_directories = '/tmp'       # comma-separated list of directories

在IPv4中添加一条语句

# IPv4 local connections:
host    all             all             0.0.0.0/0               trust
host    all             all             127.0.0.1/32            trust

九、设置PostgreSQL开机自启动

找到postgresql-14.4

[postgres@localhost postgresql-14.4]$ pwd
/opt/module/postgresql-14.4
[postgres@localhost postgresql-14.4]$ cd contrib/start-scripts/
[postgres@localhost start-scripts]$ ls
freebsd  linux  macos

切换为root用户

[root@localhost start-scripts]# chmod a+x linux
[root@localhost start-scripts]# cp linux /etc/init.d/postgresql

修改/etc/init.d/postgresql文件的两个变量

# Installation prefix
prefix=/opt/module/pgsql

# Data directory
PGDATA="/opt/module/pgsql/data"

设置postgresql服务开机自启动

[root@localhost init.d]# chkconfig --add postgresql
[root@localhost init.d]# chkconfig
...
postgresql     	0:off	1:off	2:on	3:on	4:on	5:on	6:off

然后将5432端口开启(或直接把防火墙给关了)

sudo ufw allow 5432/tcp

执行service postgresql start,启动PostgreSQL服务

[root@localhost init.d]# service postgresql start
Starting PostgreSQL: ok

十、开始测试

默认的用户是postgres,密码和linux系统中所设的postgres用户的密码一样

[root@localhost init.d]# su - postgres
Last login: Thu Aug  4 23:08:40 PDT 2022 on pts/0
[postgres@localhost ~]$ psql
psql (14.4)
Type "help" for help.

postgres=# 

你可能感兴趣的:(linux,pgsql,postgresql,ubuntu,数据库)