Being the most advanced open-source relational database, many of us need to install PostgreSQL for some purposes. However, sometimes you may not have sudo
privilege on an Ubuntu server. Then, how should you install PostgreSQL in this case?
Compile from source!
Next, we will show a step-by-step guide on how to install PostgreSQL on Ubuntu by compiling it from source.
- Make sure you already have the following software installed:
- A C/C++ compiler, such as GCC;
- Other supporting software, see here.
- Download the source from here.
- At the time of writing, you can
wget https://ftp.postgresql.org/pub/source/v12.1/postgresql-12.1.tar.gz
.
- At the time of writing, you can
- Unzip the source by
tar xvzf postgresql-12.1.tar.gz
. - Navigate into the folder
cd postgresql-12.1/
. - Update source configuration
./configure --prefix=/temp/yunpengn/postgresql --with-python PYTHON=/usr/bin/python3
. - Compile the source by
make world -j <num_cores_to_use>
.- Check the number of CPU cores on your machine by
cat /proc/cpuinfo | grep processor | wc -l
; - The argument
world
means all additional modules will also be compiled. You can ignore it. - This step may take a while if your server is not powerful enough.
- Check the number of CPU cores on your machine by
- Install PostgreSQL now by
make install-world
.
- Add PostgreSQL binaries into your path by
echo 'export PATH="/temp/yunpengn/postgresql/bin:$PATH"' >> ~/.bash_profile && source ~/.bash_profile
. - Initialize the database by
initdb -D /temp/yunpengn/postgresql/data
. - Start the database by
pg_ctl -D /temp/yunpengn/postgresql/data -l /temp/yunpengn/postgresql/server.log start
. - Connect to the database by
psql template1
, wheretemplate1
is the template database shipped with PostgreSQL. - Change password by
ALTER USER <username_here> PASSWORD 'new_password_here';
. - Remove the default database created by
DROP DATABASE postgres;
. - Ceate a new database with the same name as OS account by
CREATE DATABASE <username_here>;
. - From now on, you can connect to PostgreSQL by simply
psql
.
Dude THANK YOU for this