Postgres first steps on Ubuntu

Long story short: I was deploying one of my platforms on a virtual machine running Ubuntu 16.04.
As usual I start by installing Apache, Mod Python, my preferred editor (Geany), copy the Python files in the right place and here we reach a more complex step ... getting the database up.

Actually it is not complex but there are some things that you have to remember. If you don't do it in the correct order you might loose some time getting it right and this just happened to me yesterday so this morning I decided to write this post. Why? Because everywhere I look on the web the script is bloated with stuff that is not needed.

Here we are:

# Open terminal and install PostgreSQL as following:

sudo apt-get install postgresql libpq-dev postgresql-client postgresql-client-common

# Open pg_hba.conf  to see if everything is OK.
# Your current PostgreSQL version might be different (mine is 9.5 as you can see)
# please see yours and change the path bellow accordingly
sudo nano /etc/postgresql/9.5/main/pg_hba.conf

# You should see the following line immediately after the long comments at the beginning:
local   all             postgres                          peer

# if different please make it look with the example given, save the file (CTRL+O) and exit nano (CTRL+X)
# restart the service (not needed if you did not changed the file)
sudo service postgresql restart

# Enter PostgreSQL cursor
sudo -u postgres psql

# Now you are at the helm and can start doing the actual interesting stuff
# Get control over postgres user ... you will need it :)
ALTER USER postgres PASSWORD 'password1';


# Create your custom user
CREATE USER my_user;
ALTER USER my_user PASSWORD 'password2';

# This one depends on what you need to do with your user
ALTER USER my_user WITH SUPERUSER;

# Create the database
CREATE DATABASE my_database;
GRANT ALL privileges ON DATABASE my_database TO my_user; 


# Now you can exit postgres command line and add pgadmin3 to do the rest of the stuff using a GUI
sudo apt-get install pgadmin3

Enjoy :)

2 comments:

Clipping Path Service said...

I really like your writing style, great info, you putting so much effort into this blog. Thanks to share with us.

Clipping Path Service said...

Amazing, this is a great article! I did enjoyed reading it, keep your post .