Step 1: Prepare Your CSV File
- Ensure the file is in CSV format (
.csv
). - Make sure it has a header row (column names) that matches the table structure in PostgreSQL.
- Prepare a scheme design to show mirror the csv file showing the column_name, datatypes, constraints, and Example values
Scheme Design: Student
Column Name | Data Type | Constraints | Example Value |
student_id | SERIAL | PRIMARY KEY | 1 |
first_name | VARCHAR(50) | NOT NULL | John |
last_name | VARCHAR(50) | NOT NULL | Doe |
email | VARCHAR(100) | UNIQUE, NOT NULL | john.doe@example.com |
date_of_birth | DATE | NOT NULL | 2000-05-15 |
Scheme Design: courses
Column Name | Data Type | Constraints | Example Value |
course_id | SERIAL | PRIMARY KEY | 1 |
course_name | VARCHAR(100) | NOT NULL | Mathematics |
course_code | VARCHAR(10) | UNIQUE, NOT NULL | MATH101 |
credit_hours | INT | NOT NULL | 3 |
Scheme Design: enrollments
Column Name | Data Type | Constraints | Example Value |
enrollment_id | SERIAL | PRIMARY KEY | 1 |
student_id | INT | REFERENCES students(student_id) | 1 |
course_id | INT | REFERENCES courses(course_id) | 1 |
enrollment_date | DATE | DEFAULT CURRENT_DATE | 2025-01-24 |
Create the Database
- Open pgAdmin 4 and connect to your PostgreSQL server.
- Right-click on
Databases
→ SelectCreate
→Database
. - Name the database
school
and click Save.
Create the Tables
- Open the
Query Tool
from the toolbar or by right-clicking the database. - Use the following SQL to create the
students
,enrollment
andcourses
tables:
-- Create the students table
CREATE TABLE students (
student_id SERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
date_of_birth DATE NOT NULL
);
-- Create the courses table
CREATE TABLE courses (
course_id SERIAL PRIMARY KEY,
course_name VARCHAR(100) NOT NULL,
course_code VARCHAR(10) UNIQUE NOT NULL,
credit_hours INT NOT NULL
-- Create the enrollment table to link students and courses
CREATE TABLE enrollments (
enrollment_id SERIAL PRIMARY KEY,
student_id INT REFERENCES students(student_id) ON DELETE CASCADE,
course_id INT REFERENCES courses(course_id) ON DELETE CASCADE,
enrollment_date DATE DEFAULT CURRENT_DATE
);
Step 4: Import the CSV File
- Navigate to the table where the data will be imported:
- Expand your database → Expand
Schemas
→ ExpandTables
→ Right-click your table. - Select Import/Export Data.
Step 5: Configure the Import
- In the Import/Export Data dialog:
- Filename: Click
…
(browse) and select your CSV file. - Format: Choose
CSV
. - Encoding: Select the appropriate encoding (usually
UTF-8
). - Header: Check this box if the first row in your file is a header row.
- Delimiter: Specify the delimiter (default is
,
for CSV). - Verify that the Columns in the dialog match your table's schema.
Step 6: Execute the Import
- Click OK to begin the import process.
- A message will appear confirming the successful import or any errors.
Step 7: Verify the Data
- Query your table to confirm the data has been imported correctly:
SELECT * FROM student;
Notes and Troubleshooting
- CSV Formatting:
- Ensure that the number of columns in the CSV matches the table.
- Use quotes (
"
) for text values if they contain delimiters. - Permissions:
- Ensure you have write access to the table and the file path is accessible.
- Error Handling:
- If there’s an error, check the Log Output in pgAdmin for details.