Installing PostgreSQL on Ubuntu 18.04

PostgreSQL is a popular database used with many development stacks and languages. You can install PostgreSQL on your Ubuntu 18.04 system using the following steps.

PostgreSQL is included in the official repositories of Ubuntu 18.04 so you can simpy use the official apt package manager by running the following commands in your terminal:

$ sudo apt update
$ sudo apt install postgresql postgresql-contrib

You first update the cache in your system using the apt update command, next you install the postgresql postgresql-contrib package using the apt install command.

By default a user account called postgres is created for you when setup is finished. You need to log into this default account to be able to run PostgreSQL commands using the following command:

$ sudo -i -u postgres

After running the command, you'll be able to interact with PostgreSQL via psql. First, run the following command in your terminal:

$ psql

You can connect to a specified database using:

$ psql -d postgres

postgres is a database created by default in your system after installing PostgreSQL.

Creating a New PostgreSQL Role

At this point, you only have one postgres role in your system. But, you can also create new roles from your terminal with using the createrole command. Make sure you are logged in as the postgres user and the run the following command:

$ createuser --interactive

The --interactive option will allow the command to interactively ask you for the name of the role and if it needs have superuser access.

You can also use sudo with the postgres user to run the previous command:

$ sudo -u postgres createuser --interactive

Creating a PostgreSQL Database

You can easily create a PostgreSQL database from your terminal using the createdb command. Just make sure you are logged in as a PostgreSQL user (bu default postgres if you didn't create your own user) or also run sudo with your PostgreSQL user before your command:

$ sudo -u postgres createdb mydb

You can now create database tables and data using SQL. For example to create a table run the following SQL command in the psql prompt:

CREATE TABLE test (
    test_id serial PRIMARY KEY,
    name varchar (100) NOT NULL,
    email varchar (100)
);

Conclusion

In this tutorial, you have installed and configured the PostgreSQL database on your Ubuntu 18.04 system.


Author


SUBSCRIBE


JOIN OUR COMMUNITY!


comments powered by Disqus