[LINUX] Install PostgreSQL from source code on CentOS

Background

I wanted to install it easily with package management (yum, apt, etc.), but I had the opportunity to install PostgreSQL on CentOS 7.5 for the first time in about 20 years from the source code, so I made a note.

environment

# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

procedure

For the time being, I was able to do it by the following procedure.

Account preparation

Prepare a dedicated account to use PostgreSQL. Create an account name "postgres" by also specifying your home directory.

# useradd -m -d /home/postgres postgres

You will also set a password.

# passwd postgres
Change password for user postgres.
new password:
Please re-enter your new password:
passwd:All authentication tokens have been successfully renewed.

Preparation of necessary tools

Prepare the necessary tools to get the source code and compile it. (I will definitely enjoy yum here!)

# yum -y groupinstall Development Tools
# yum -y install readline-devel
# yum -y install zlib-devel zlib wget vim

Get PostgreSQL source code

Get it with wget. I was able to check the version list on the official site, so I used v12.4 this time. PostgreSQL File Browser

# wget https://ftp.postgresql.org/pub/source/v12.4/postgresql-12.4.tar.gz
...
100%[==========================================================>] 27,070,070   564KB/s time 72s
2020-09-30 17:46:23 (365 KB/s) - `postgresql-12.4.tar.gz'Save to[27070070/27070070]

Extraction of source code

Since it is hardened with tar, it expands softly.

# tar xvfz postgresql-12.4.tar.gz
...
postgresql-12.4/configure.in
postgresql-12.4/INSTALL

Compilation work

Compiling the extracted source code. (I feel nostalgic after working for the first time in 20 years)

# cd postgresql-12.4
# ./configure
# make
# make install
...
PostgreSQL installation complete.

Account switching

After the installation work, PostgreSQL is operated with the account "postgres" created earlier.

# exit
login:postgres
Password:
$ 

Set environment variables

Since the PATH to the installed PostgreSQL does not pass, edit .bash_profile etc. Put it in your PATH.

$ vim .bash_profile

Editing contents ↓

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH=/usr/local/pgsql/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH

Logout → You can log in again, but it was troublesome, so reload it with source.

$ source .bash_profile

Let's check if there are any commands related to PostgreSQL.

$ which psql
/usr/local/pgsql/bin/psql

$ which initdb
/usr/local/pgsql/bin/initdb

I'm on the PATH!

Try creating a database cluster.

In the case of PostgreSQL, it is necessary to create a database cluster first, so create it while specifying the directory.

$ initdb -D /home/postgres/data --no-locale
...
Success. You can now start the database server using:
    pg_ctl -D /home/postgres/data -l logfile start

$ ls -l /home/postgres
4 in total
drwx------.19 postgres postgres 4096 September 30 18:12 data

Try starting a database cluster.

Specify the created database cluster and start PostgreSQL.

$ pg_ctl start -D /home/postgres/data
...
 done
server started
$ 

Connect to the database with psql and hit the SQL statement!

Since it started, I will try to hit the SQL statement. Connect with psql for the time being.

$ psql
psql (12.4)
Type "help" for help.
postgres=#

It seems that you can enter SQL statements, so you can enter it as a trial.

postgres=# SELECT now();
              now
-------------------------------
 2020-09-30 18:20:14.031968+09
(1 row)
postgres=# \q
$  

done. It's been a while> PostgreSQL

Summary

To be honest, it's easy to prepare with a package management tool. If you need to compile from the source code for some reason, you have to work while looking at the manual, so let's do our best!

Chapter 16 Install from Source Code psql document

Recommended Posts

Install PostgreSQL from source code on CentOS
Install ansible from source code
Install Faiss on CentOS 7
Build PostgreSQL from source
Install python from source
Install Python3.4 on CentOS 6.6
Install Apache 2.4.41 from source
Install mecab-python on CentOS
Install Python 2.7.3 on CentOS 5.4
Install awscli on centos7
Install Chainer on CentOS 6.7
Install ImageMagick-6.2.x series on CentOS7.7
Install Python 3.8 on CentOS 7 (SCL)
wsl Install PostgreSQL on Ubuntu 18.04
Install Chrome on CentOS 7 series
Install Python 3.8 on CentOS 8 (AppStream)
Install Apache Maven (from source)
Steps to install MySQL 8.0 on CentOS 8.1
raspberry pi 4 centos7 install on docker
Install Python from source with Ansible
Steps to install VirtualBox on CentOS
Install java (Oracle JDK14) on CentOS7
How to install PyPy on CentOS
How to install TensorFlow on CentOS 7
Install pip on CentOS7. Also iPython.
Install VirtualBox on CentOS 7 on VirtualBox (mac + vagrant)
Install Python on CentOS using Pyenv
Install VS Code on your Chromebook
Compile and install Git from source.
How to install Maven on CentOS
Study Note 9_Install Jenkins on CentOS7
Install Python on CentOS using pyenv
Enable sar command on CentOS (install sysstat)
Connecting from python to MySQL on CentOS 6.4
Install Pleasant on Ubuntu 20.04 (.NetCore3.1 / PostgreSQL version)
Install Mecab on Linux (CentOS) with brew
Flow from source code to creating executable
How to install Apache (httpd) on CentOS7
How to install Eclipse GlassFish 5.1.0 on CentOS 7
How to install Apache (httpd) on CentOS8
Install pyenv and rbenv on CentOS system-wide
Install the latest version of Apache httpd 2.4 from source on Cent OS 8
Install openstack client from pip so you don't get an error on CentOS7
Speak voice from code on mac (eSpeak, OpenJTalk)
[Ansible] Install dnf on Centos7 with Python3 interpreter
CentOS8 --Install --Django
Install from conda-forge
Boot CentOS 8 from Windows 10 with Wake On LAN
Install vim7.3 (+ python2.4) from source (compatible with Gundo.vim)
[Failure] Install Stack Overflow Clone Askbot on CentOS 6.4
CentOS8 --Install --Python3
[Python] Django Source Code Reading View Starting from Zero ①
Install Grub on USB memory (UEFI) ~ Boot Linux from grub on USB memory ~
How to install Git GUI and Gitk on CentOS
Install mecab on Marvericks
Install Tensorflow on Mac
Install python on WSL
Install pip on Mavericks
Install Python on Pidora.
Install mongodb on termux
Install docker on Fedora31