cd Downloads/Create File
touch install-postgresql.shopen the file with txt file and copy-paste the bellow code -
sudo apt install curl ca-certificates
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
. /etc/os-release
sudo sh -c "echo 'deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $VERSION_CODENAME-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
sudo apt update
sudo apt -y install postgresqlMake the code execute :
chmod +x install-postgresql.shAfter saved then back to the terminal and run that command :
./install-postgresql.shgoto postgres terminal
sudo -i -u postgresyour terminal look like : postgres@hp-elitebook-840-g4:~$
then connect with postgre serve just type
psql- Now you can access database and your terminal look like start
postgres=#- Create Database
CREATE DATABASE my_database;Check database created or not : by typing \l
postgres=# \lpress q to exit and back to postgres=#
- To connected Database by
\c my_database
postgres=# \c my_databaseNow your terminal look like start
my_database-#- Now If you want to quit just write -
\q.
my_database-# \qreturn back to postgres@hp-elitebook-840-g4:~$
Open terminal and just follow the command given below
#
# Setup the repository
#
# Install the public key for the repository (if not done previously):
curl -fsS https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /usr/share/keyrings/packages-pgadmin-org.gpg
# Create the repository configuration file:
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/packages-pgadmin-org.gpg] https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'
#
# Install pgAdmin
#
# Install for both desktop and web modes:
sudo apt install pgadmin4
# Install for desktop mode only:
sudo apt install pgadmin4-desktop
# Install for web mode only:
sudo apt install pgadmin4-web
# Configure the webserver, if you installed pgadmin4-web:
sudo /usr/pgadmin4/bin/setup-web.shgoto Downlaod Folder or others and create this file :
touch install-pgadmin.shTo executable :
chmod +x install-pgadmin.shInstall just type :
./install-pgadmin.shRef-1: https://www.pgadmin.org/download/pgadmin-4-apt/
Ref-2: https://www.youtube.com/watch?v=c_VFpEec5C4
- if your previous db is
my_databaseand want to rename just typealter database my_database rename to ph_db;
postgres=# alter database my_database rename to ph_db;-
if your db name is
test_dbpostgres=# drop database test_db;
ALTER TABLE table_name action;sudo -i -u postgrespsql\password postgresThen enter and confirm a new password.
\q
exitpsql -U postgres -WSHOW TIMEZONE;👉 সার্ভারে সেট করা ডিফল্ট টাইমজোন দেখায়।
SELECT now();👉 বর্তমান তারিখ ও সময় (timestamp with time zone) রিটার্ন করে।
SELECT * FROM timeZ;👉 timeZ টেবিলের সব রেকর্ড দেখায়।
SELECT CURRENT_DATE;👉 শুধু আজকের তারিখ দেখায় (সময় ছাড়া)।
SELECT now()::date;👉 now() থেকে কেবল তারিখ অংশ বের করে।
SELECT now()::time;👉 now() থেকে কেবল সময় অংশ বের করে।
SELECT to_char(now(), 'YYYY-MM-DD');👉 আজকের তারিখ YYYY-MM-DD ফরম্যাটে স্ট্রিং আকারে রিটার্ন করে (যেমন 2025-05-17)।
SELECT to_char(now(), 'DDD');👉 বছরে আজকের দিনটি কততম দিন তা দেখায় (যেমন 137তম দিন)।
SELECT CURRENT_DATE - INTERVAL '1 year 2 month';👉 আজকের তারিখ থেকে ১ বছর ২ মাস বাদ দিয়ে রেজাল্ট দেয়।
SELECT age(CURRENT_DATE, '1995-08-26');👉 আজকের তারিখ থেকে '1995-08-26' তারিখ পর্যন্ত কত বয়স হয়েছে তা দেখায় (বছর, মাস, দিন আকারে)।
SELECT *, age(CURRENT_DATE, dob) AS student_age FROM students;👉 students টেবিলের প্রতিটি ছাত্রের জন্মতারিখ (dob) থেকে বর্তমান বয়স বের করে student_age নামে দেখায়।
SELECT extract(month from '2025-05-25'::date);👉 '2025-05-25' তারিখ থেকে মাস (May → 5) বের করে।
-
Cascading Deletion --> ON DELETE CASCADE
user_id INTERGER REFERENCES "user"(id) ON DELETE CASCADE
-
Setting NULL --> ON DELETE SET NULL
user_id INTERGER REFERENCES "user"(id) ON DELETE set null
-
Restrict Deletion --> ON DELETE RESTRICT / ON DELETE NO ACTION (default)
user_id INTERGER REFERENCES "user"(id) ON DELETE set DEFAULT 2
-
Set Default value --> ON DELETE SET DEFAULT
user_id INTERGER REFERENCES "user"(id)
Visit : https://like-frog-b41.notion.site/SQL-Join-Practice-Task-27ac979408f5477da80de4ab299f9225
Repository Files : https://github.com/Apollo-Level2-Web-Dev/dbms-postgres
-- 1. Inner Join to Retrieve Employee and Department Information
SELECT * FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
-- 2. Group By Department with Average Salary
SELECT departments.department_name, ROUND(AVG(employees.salary)) AS average_salary
FROM employees
INNER JOIN departments ON employees.department_id = departments.id
GROUP BY departments.department_name;
-- 3. Count Employees in Each Department
SELECT departments.department_name, COUNT(employees.id) AS employee_count
FROM employees
JOIN departments ON departments.id = employees.department_id
GROUP BY departments.department_name;
-- 4.Find the Department name with the Highest Average Salary
SELECT departments.department_name, ROUND(AVG(employees.salary)) AS Highest_AVG_Salary
FROM employees
JOIN departments ON departments.id = employees.department_id
GROUP BY departments.department_name
ORDER BY Highest_AVG_Salary DESC
LIMIT 1;
-- 5. Count Employees Hired Each Year
SELECT EXTRACT(YEAR FROM hire_date) AS hire_year, count(employee_name) AS total_hired
FROM employees
GROUP BY hire_year
ORDER BY hire_year DESC;
-- 6. Find customers who have placed more than 2 orders and calculate the total amount spent by each of these customers.
SELECT customer_id, SUM(total_amount) AS total_spent
FROM orders
GROUP BY customer_id
HAVING COUNT(customer_id) > 2
ORDER BY total_spent DESC;
-- 7: Find the total amount of orders placed each month in the year 2022.
SELECT SUM(total_amount) AS totalAmount, EXTRACT(MONTH FROM order_date) AS order_month
FROM orders
WHERE EXTRACT(YEAR FROM order_date) = 2022
GROUP BY order_month
ORDER BY order_month;