Skip to content

Requirements

How to install PostgreSQL

Linux

If you are on Arch Linux you can use the following command:

sudo pacman -S postgresql

to install it, but if you are on other Linux distributions just change the Package Manager.

Note

If you have never used PostgreSQL on Linux I suggest you follow this guide.

macOS

If you are on macOS you can use Homebrew to install PostgreSQL with the following command:

brew install postgresql

or you can use MacPorts with the following command:

sudo port install postgresql16

Configure PostgreSQL

Linux

After the installation of PostgreSQL you need to use the following commands:

sudo -u postgres initdb -D /var/lib/postgres/data
sudo mkdir -p /var/lib/postgres/data
sudo chown postgres:postgres /var/lib/postgres/data

to initialize the database and create the necessary directories. Then you can use the following command to start the PostgreSQL service:

sudo systemctl start postgresql

and to enable it at startup use the following command:

sudo systemctl enable postgresql

How to create a database

Once you have downloaded PostgreSQL you need to create the todolist database. Open a terminal and write:

sudo -i -u postgres

this command allows you to change your computer user by selecting the postgres user. Now use the following command to create a database called todolist:

createdb todolist

Now run the command:

# Linux
psql

# macOS
psql postgres

you will get something like this:

psql (16.2)
Type "help" for help.

postgres=#

then write:

\c todolist

you should get a message similar to this:

You are now connected to database "todolist" as user "postgres".

Now if you open the src/table.sql file you will find this code inside:

1
2
3
4
5
-- Use the following command to create a tasks table on todolist database
CREATE TABLE tasks (
    id SERIAL PRIMARY KEY,
    description TEXT NOT NULL
);

compact this code as follows:

CREATE TABLE tasks (id SERIAL PRIMARY KEY, description TEXT NOT NULL);

and write it immediately after todolist=# and you should get this message:

CREATE TABLE

Now use the following SQL code to see the description and id columns that are part of the tasks table:

SELECT * FROM tasks;

and in particular you should get something similar to this:

id | description 
----+-------------
(0 rows)

Now that you have successfully created the todolist database and created a tasks table to place the information in, you can exit psql using the \q command and then you can switch back to the main user using the exit command.