Postgres setup guide

2018-05-08

Short guide about how to setup postgres on linux without pain and system pollution using single directory.

Download Postgres binaries from here (I don’t like to install from repos, because it pollutes system).

Extract archive content to some folder:

tar -xvzf postgresql-10.4-1-linux-x64-binaries.tar.gz 

Configure PATH variable. Add following code to your ~/.bashrc file:

POSTGRES_HOME=/home/user/.stack/pgsql
export PATH="$POSTGRES_HOME/bin:$PATH"

Restart terminal or just execute source ~/.bashrc. Now you have postgres binaries in your $PATH so u can call them everywhere.

Create folder where you want postgres keep its files:

mkdir /home/user/pg_data

Initialize folder with postgres:

initdb /home/user/pg_data

Start the database server:

pg_ctl -D /home/user/pg_data -l pg_data/logfile start

You can stop database server by following command:

pg_ctl -D /home/user/pg_data stop

Create the user postgres with super user privileges:

createuser --superuser postgres

Create data base:

createdb --echo-all --owner=postgres testdb

Now you can connect to db with command:

psql --echo-all --dbname testdb --user postgres