Skip to content

Latest commit

 

History

History
151 lines (89 loc) · 5.26 KB

README.md

File metadata and controls

151 lines (89 loc) · 5.26 KB

Using PostgreSQL on Linux (Ubuntu)

A guide for installing, setting up, and using PostgreSQL on Linux (Ubuntu).

What is PostgreSQL

PostgreSQL is a Relational Database Management System (RDBMS) that enables you to organize, store, and manage data in structured objects with tables and allows relationships between these tables.

What You Should Know About PostgreSQL on Ubuntu

According to the official PostgreSQL download page, PostgreSQL is by default available and supported by all Ubuntu versions. It is one of the reasons why I recommend using this RDBMS over any other open source (RDBMS) options on Ubuntu. Nonetheless, you may have your personal preference/favorite for different reasons.

Installing PostgreSQL on Linux (Ubuntu)

Let's get to it, shall we?

  1. Open your terminal

  2. According to the official PostgreSQL download page, you should run the following command:

    apt install postgresql
    
  • But you may run into a Permission denied error:

    image

  • In that case, run the command as a superuser with sudo:

    sudo apt install postgresql
    

    image

  1. Explore more ways to install postgreSQL on Ubuntu for automatic updates throughout the support lifetime of PostgreSQL in the official PostgeSQL download page

Setting Up PostgreSQL

  • To use PostgreSQL seamlessly, we will need to create a user.

Creating a User on PostgreSQL

  1. Switch to the PostgreSQL Server by running the following command on your terminal:

    sudo -i -u postgres
    

    image

  2. To create a user with a password, run the following command (also see option 3 below):

    createuser NameOfUserHere -P
    
  • Replace NameOfUserHere with your preferred name for the new user.

  • You will be prompted to enter a password for the new user and confirm it.

  • When you're done creating the new user, type exit and press ENTER to exit the postgres server.

    image

  1. An alternative (recommended) approach is to use the --interactive flag.
  • Run the following command:

    createuser -P --interactive
    
  • This will prompt you to enter the following:

    • Name of the role/user you want to create
    • The password and password confirmation for this new user
    • Whether to assign a superuser role to this new user (recommended if you haven't created any other user)

Creating an Initial Database

When you're done creating the new user, you will need to create an initial database that matches the name of your new user. Otherwise, you will not be able to access the postgreSQL shell.

alt text

For instance, if above you created a user called benie, your initial database should be called benie.

Follow the steps below to create your initial database:

  1. Run the PostgreSQL Server
  • In case you had terminated your server session, run the following command to restart it:

    sudo -i -u postgres
    
  1. Create an Initial Database
  • Create your initial database using the same name you used for your new user:

    createdb NameOfYourNewUserHere;
    
  • Replace NameOfYourNewUserHere with the name you used to create your new user in (3) above, e.g. benie.

    alt text

  • Once you've created your initial database, type exit and press ENTER to exit the postgres server.

Creating Subsequent Databases with PostgreSQL

After creating a user with an initial database, you will have access to the postgreSQL shell and may proceed to create more databases.

  1. Run the following command to open a postgreSQL shell:

    psql
    

    image

  2. Run the following command to create a new database

    CREATE DATABASE nameOfYourNewDb;
    
  • Replace nameOfYourDb with your preferred name for your database.

  • Please remember to add a semicolon ; after each line.

    image

  • When you've created your new database, type \q and press ENTER to exit the postgres shell.

Success

That's it! You've successfully:

  • Created a postgreSQL user
  • Your initial database
  • Your first subsequent database (after the initial)

You can use always use your subsequent databases locally to store data.

PostgreSQL Official Documentation

Check out the official PostgreSQL documentation for more ways that you can use and interact with PostgreSQL.

Using Your New PostgreSQL Database

Check out the following guide to learn how to use your new database in a Python- Django application.