Skip to content

Instantly share code, notes, and snippets.

@yunpengn
Last active May 20, 2024 06:57
Show Gist options
  • Save yunpengn/832aceac6998e2f894e5780229920cb5 to your computer and use it in GitHub Desktop.
Save yunpengn/832aceac6998e2f894e5780229920cb5 to your computer and use it in GitHub Desktop.
A step-by-step guide to install PostgreSQL on Ubuntu without sudo privilege

Install PostgreSQL on Ubuntu without sudo

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.

PostgreSQL Compilation & Installation

  • 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.
  • 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.
  • Install PostgreSQL now by make install-world.

PostgreSQL Setup

  • 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, where template1 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.

References

@kaubu
Copy link

kaubu commented Mar 5, 2022

Dude THANK YOU for this

@van31337
Copy link

I'm literally looking for this, tysm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment