源码包下载地址
https://www.postgresql.org/ftp/source/
下载源码包
wget https://ftp.postgresql.org/pub/source/v14.2/postgresql-14.2.tar.gz
安装编译所需要的软件包
apt update && apt install -y gcc make libreadline-dev zlib1g-dev
解压postgresql,并进入到postgresql源码目录里
root@postgresql-1:~# tar -xf postgresql-14.2.tar.gz root@postgresql-1:~# cd postgresql-14.2
查看帮助编译步骤和编译参数
root@postgresql-1:~/postgresql-14.2# cat INSTALL |less 返回结果 。。。。。 Short Version ./configure make su make install adduser postgres mkdir /usr/local/pgsql/data chown postgres /usr/local/pgsql/data su - postgres /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start /usr/local/pgsql/bin/createdb test /usr/local/pgsql/bin/psql test The long version is the rest of this document. 。。。。。 编译参数帮助信息 root@postgresql-1:~/postgresql-14.2# ./configure --help
编译安装posetgresql
1.编译参数选项 ./configure --prefix=/usr/local/pgsql 解释 --prefix=/usr/local/pgsql 把postgre安装到/usr/local/pgsql下 2.编译和编译安装 make -j 4 world && make install-world 解释: make #默认不包括文档和其它模块,4表示当产主机的CPU核心数 make install-world #默认 make install 不包括安装文档
创建postgres用户,并设置密码为123456
1.创建用户 useradd -s /bin/bash -m -d /home/postgres postgres 2.设置密码 echo -e '123456\n123456' | passwd postgres
创建数据库存放目录
1.创建postgresql数据存放目录 root@postgresql-1:~/postgresql-14.2# mkdir -pv /pgsql/data 2.授权postgres的属主属组 root@postgresql-1:~/postgresql-14.2# chown postgres.postgres /pgsql/data/
添加环境变量,方便后期调取命令和目录等操作
1.添加环境变量 vim /etc/profile.d/pgsql.sh 写入一下内容 export PGHOME=/usr/local/pgsql export PATH=$PGHOME/bin/:$PATH export PGDATA=/pgsql/data export PGUSER=postgres export MANPATH=/usr/local/pgsql/share/man:$MANPATH 2.让环境变量生效 source /etc/profile.d/pgsql.sh
初始化数据库,首先把用户切换到postgres用户下,执行初始化命令
1.切换到postgres用户下 root@postgresql-1:~/postgresql-14.2# su - postgres 2.执行初始化命令 root@postgresql-1:~/postgresql-14.2# su - postgres 返回结果: postgres@postgresql-1:~$ /usr/local/pgsql/bin/initdb -D /pgsql/data/ 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 /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 ... Etc/UTC 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: /usr/local/pgsql/bin/pg_ctl -D /pgsql/data/ -l logfile start 3.启动postgresql postgres@postgresql-1:~$ /usr/local/pgsql/bin/pg_ctl -D /pgsql/data/ -l logfile start 返回结果: waiting for server to start.... done server started 解释: -l logfile 指定日志文件,日志在当前目录下 4.进入postgres数据库命令行 postgres@postgresql-1:~$ psql psql (14.2) Type "help" for help. postgres=#
设置开机自启动,用ruut用户添加开机服务
1.添加服务脚本,需要用root执行如下命令 cat > /lib/systemd/system/postgresql.service <<EOF [Unit] Description=PostgreSQL database server After=network.target [Service] User=postgres Group=postgres ExecStart=/usr/local/pgsql/bin/postmaster -D /pgsql/data ExecReload=/bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target EOF 2.设置开机自启动,并启动服务 systemctl daemon-reload systemctl enable postgresql.service --now