Skip to content

[SQL-428] Checking Database Connectivity with a script #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sql-queries-10/checking-database-connectivity/mysql.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASS=MySQL2024
MYSQL_DB=University
5 changes: 5 additions & 0 deletions sql-queries-10/checking-database-connectivity/postgresql.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DB_HOST=localhost
DB_PORT=5432
DB_USER=user
DB_PASS=Password2024
DB_NAME=University
17 changes: 17 additions & 0 deletions sql-queries-10/checking-database-connectivity/test-db-connect.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# PostgreSQL Database Connectivity Check Script

# Load environment variables
source postgresql.env

echo "Testing PostgreSQL connectivity..."

# Test PostgreSQL connectivity and check for 'student' table
PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c '\dt student' >/dev/null 2>&1

if [ $? -eq 0 ]; then
echo "PostgreSQL: Connection successful and student table exists."
else
echo "PostgreSQL: Connection failed or student table does not exist."
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# MySQL Database Connectivity Check Script

# Load environment variables
source mysql.env

echo "Testing MySQL connectivity..."

# Test MySQL connectivity and check for 'Department' table
mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" -e "USE $MYSQL_DB; SHOW TABLES LIKE 'Department';" >/dev/null 2>&1

if [ $? -eq 0 ]; then
echo "MySQL: Connection successful and department table exists."
else
echo "MySQL: Connection failed or department table does not exist."
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import mysql.connector
from dotenv import load_dotenv

# Load environment variables from mysql.env
load_dotenv("mysql.env")

MYSQL_HOST = os.getenv("MYSQL_HOST")
MYSQL_PORT = os.getenv("MYSQL_PORT")
MYSQL_USER = os.getenv("MYSQL_USER")
MYSQL_PASS = os.getenv("MYSQL_PASS")
MYSQL_DB = os.getenv("MYSQL_DB")

try:
connection = mysql.connector.connect(
host=MYSQL_HOST,
port=MYSQL_PORT,
user=MYSQL_USER,
password=MYSQL_PASS,
database=MYSQL_DB
)

cursor = connection.cursor()
cursor.execute("SHOW TABLES LIKE 'Department';")
result = cursor.fetchone()

if result:
print("MySQL: Connection successful and department table exists.")
else:
print("MySQL: Connection successful but department table does not exist.")

except mysql.connector.Error as err:
print(f"MySQL: Connection failed - {err}")

finally:
if 'connection' in locals() and connection.is_connected():
connection.close()
37 changes: 37 additions & 0 deletions sql-queries-10/checking-database-connectivity/test_pg_connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import psycopg2
from dotenv import load_dotenv

# Load environment variables from postgresql.env
load_dotenv("postgresql.env")

DB_HOST = os.getenv("DB_HOST")
DB_PORT = os.getenv("DB_PORT")
DB_NAME = os.getenv("DB_NAME")
DB_USER = os.getenv("DB_USER")
DB_PASS = os.getenv("DB_PASS")

try:
connection = psycopg2.connect(
host=DB_HOST,
port=DB_PORT,
dbname=DB_NAME,
user=DB_USER,
password=DB_PASS
)

cursor = connection.cursor()
cursor.execute("SELECT to_regclass('public.student');")
table_exists = cursor.fetchone()[0]

if table_exists:
print("PostgreSQL: Connection successful and student table exists.")
else:
print("PostgreSQL: Connection successful but student table does not exist.")

except Exception as e:
print(f"PostgreSQL: Connection failed - {e}")

finally:
if 'connection' in locals():
connection.close()