Setting up a PostgreSQL Database and Designing an Appropriate Schema.

Emmanuelibok
4 min readApr 4, 2024

--

Setting up a PostgreSQL database and designing an appropriate schema involves several steps. Here’s a guide to help you through the process:

To set up a PostgreSQL database instance on an EC2 Ubuntu server, follow these steps:

Step 1: Update Your Ubuntu System

  • SSH into your EC2 instance using the following command:
ssh -i your-key.pem ubuntu@your-ec2-ip
  • Once connected, update the package list and upgrade your system:
sudo apt update
sudo apt upgrade

Step 2: Install PostgreSQL

  • Install the PostgreSQL server package:
sudo apt install postgresql

Step 3: Configure PostgreSQL

  • Access the PostgreSQL command-line interface:
psql
  • Create a new database: e.g, temperature-database.
CREATE DATABASE temperature-database;
  • Create a new user and set a password: e.g, my user is Temperature.
CREATE USER Temperature WITH ENCRYPTED PASSWORD 'xxxxxxxxxxxxxxx';
  • Grant privileges to the user on the database: e.g my database_name and database use_name.
GRANT ALL PRIVILEGES ON DATABASE temperature-database TO Temperature;

Step 5: Test the Connection

  • Exit the PostgreSQL command-line interface:
\q
  • Switch back to the “ubuntu” user:
exit
  • Test the connection to the PostgreSQL server using the created user:
psql -U Temperature -d temperature-database -h ip_adress or localhost 
  • Enter the password for the user when prompted.
  • You should now be connected to the PostgreSQL server.

Designing Schema for your database.

1. Identify Data Requirements:

Before creating a database schema to store temperature data (in °C) for cities such as Zurich, London, Miami, Tokyo, and Singapore, it’s important to thoroughly analyze the data and its specific requirements. This analysis may include understanding the frequency of data updates, the range of temperatures to be recorded, any additional metadata needed (such as timestamps or geographical information), and the potential queries that will be performed on the data.

2. Entity-Relationship Diagram (ERD):

Create an Entity-Relationship Diagram to visualize the relationships between different entities. Identify entities, their attributes, and relationships between them.

Normalize the Schema:

Normalize the schema to reduce redundancy and ensure data integrity. Common normalization forms include 1NF, 2NF, and 3NF.

Define Tables and Relationships:

Based on your ERD and normalization, define tables and their relationships. Use appropriate data types for each column (e.g., integer, text, timestamp).

Example Schema:

Consider a temperature tracking system schema with the following entities:

Regions:

region_id (Primary Key) name description Cities:

city_id (Primary Key) region_id (Foreign Key) name latitude longitude Temperatures:

temperature_id (Primary Key) city_id (Foreign Key) date temperature_celsius In this schema:

Follow these steps to set up a PostgreSQL database instance on your EC2 Ubuntu server. If you find this guide useful, please consider liking, sharing, and leaving a comment below

Implement Schema in PostgreSQL

  • Here’s an example schema:
CREATE TABLE Temperature-Data-Table (
id SERIAL PRIMARY KEY,
city VARCHAR(50),
temperature DECIMAL(5,2), -- Assuming temperature data is in Celsius
recorded_at TIMESTAMP
);

With this schema, you have a single table Temperature-Data-Table to store temperature readings for all cities. Each row in the table represents a single temperature reading for a specific city at a specific time.

  • Example data insertion:
INSERT INTO Temperature-Data-Table (city, temperature, recorded_at) 
VALUES
('Zurich', 15.5, '2024-04-04 12:00:00'),
('London', 10.2, '2024-04-04 12:00:00'),
('Miami', 28.7, '2024-04-04 12:00:00'),
('Tokyo', 20.3, '2024-04-04 12:00:00'),
('Singapore', 30.1, '2024-04-04 12:00:00');

To run the SQL script to create the schema and insert data into the PostgreSQL database, you can follow these steps:

1. Connect to PostgreSQL:

  • You need to connect to your PostgreSQL database using a client such as psql command-line tool or a graphical interface like pgAdmin.
  • If you’re using psql command-line tool, you can connect to

PostgreSQL by running:

psql -U username -d database_name -h hostname

2. Run the SQL Script:

Once connected to the PostgreSQL database, you can run the SQL script that contains the schema definition and data insertion statements.

If the script is saved in a file named temperature_data_schema.sql, you can run it using the \i meta-command in psql:

\i path/to/temperature_data_schema.sql

3. Verify Data:

After running the script, you can verify that the schema and data have been successfully created by querying the database:

SELECT * FROM Temperature-Data-Table;

This schema allows you to store temperature data for multiple cities efficiently. You can query this table to retrieve temperature data for a specific city, for a specific time period, or to perform analyses on temperature trends over time for different cities.

Thank you .

--

--

Emmanuelibok

As a DevOps pro, I automate workflows, boost reliability, and foster teamwork. My goal: agile, secure, and efficient software delivery.