Foreword:
Why am I writing this article?
I have been learning PgSQL recently, and I looked for some reference articles and found that most of them are 12.x. I want to learn the new version, so I came up with the content of this article.
1. Download the pgsql installation package
https://ftp.postgresql.org/pub/source/v14.5/postgresql-14.5.tar.gz
md5:
https://ftp.postgresql.org/pub/source/v14.5/postgresql-14.5.tar.gz.md5
2. Create the pgsql directory
mkdir -p /www/server/pgsql/data
mkdir -p /www/server/pgsql/logs
3. Install pre-dependencies
apt-get install zlib1g zlib1g-dev
4. Unzip the installation package
tar zxvf postgresql-14.5.tar.gz -C /usr/local/
cd /usr/local/postgresql-14.5
./configure --prefix=/www/server/pgsql --without-readline
echo $?
gmake -j 16 world
echo $?
gmake install-world
echo $?
Note: echo $? is to check whether there are errors in each step of the process. gmake -j 16 My server has 16 cores. Select full core compilation, which is faster.
5. Add pgsql account
mkdir /home/postgress -p
groupadd postgres
useradd postgres -g postgres -m /home/postgres
chown postgres:postgres /home/postgress -R
chown postgres:postgres /www/server/pgsql -R
chmod 700 /www/server/pgsql/data -R
6. Switch postgres account
su - postgres
vim /home/postgres/.bash_profile
插入下面的内容
export PGDATA=/www/server/pgsql/data
export LANG=en_US.utf8
export install_dir=/www/server/pgsql/
export LD_LIBRARY_PATH=$install_dir/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export DATA=date +"%Y%m%d%H%M" export PATH=$install_dir/bin:$PATH:. export MANPATH=$install_dir/share/man:$MANPATH export PGUSER=postgres
Press :wq! After saving,
execute
source /home/postgres/.bash_profile
bash
Enter psql --version
Display pgsq version description environment variables added correctly
7. Initialization data
initdb -D /www/server/pgsql/data
In a production environment, it is recommended to initialize like this:
initdb -A md5 -D /www/server/pgsql/data -E utf8 --locale=C -W
<!--Parameter interpretation:
-W prompt for a password for the new superuser Set password for super administrator postgres
-A, --auth=METHOD default authentication method for local connections The default authentication method for local connections is locale
-D, --pgdata=]DATADIR location for this database cluster The location where the database data is stored
-->
8. Start pgsql
pg_ctl -D /www/server/pgsql/data start
Close pgsql correctly
pg_ctl -D /www/server/pgsql/data stop -mf
Reference tutorial: