-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b49ae33
commit 514211f
Showing
3 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import psycopg2 | ||
from psycopg2 import sql | ||
|
||
# Connection parameters | ||
DB_HOST = "geoboundaries-postgres-service" # Service name resolves to the ClusterIP | ||
DB_PORT = "5432" # Default PostgreSQL port | ||
DB_NAME = "geoboundaries" # Database name | ||
DB_USER = "geoboundaries" # Username | ||
DB_PASSWORD = "" # Empty password since host auth is trust | ||
|
||
def test_postgis_connection(): | ||
try: | ||
# Establish connection | ||
print("Connecting to the PostGIS database...") | ||
conn = psycopg2.connect( | ||
dbname=DB_NAME, | ||
user=DB_USER, | ||
password=DB_PASSWORD, | ||
host=DB_HOST, | ||
port=DB_PORT | ||
) | ||
print("Connection successful!") | ||
|
||
# Create a cursor object | ||
cursor = conn.cursor() | ||
|
||
# Test if PostGIS extension is enabled | ||
cursor.execute("SELECT PostGIS_Version();") | ||
postgis_version = cursor.fetchone() | ||
if postgis_version: | ||
print(f"PostGIS Version: {postgis_version[0]}") | ||
else: | ||
print("Failed to fetch PostGIS version. Is PostGIS enabled?") | ||
|
||
# Test a basic query | ||
cursor.execute("SELECT current_database();") | ||
db_name = cursor.fetchone()[0] | ||
print(f"Connected to database: {db_name}") | ||
|
||
# Close the cursor and connection | ||
cursor.close() | ||
conn.close() | ||
print("Connection closed successfully.") | ||
|
||
except Exception as e: | ||
print(f"Error: {e}") | ||
|
||
if __name__ == "__main__": | ||
test_postgis_connection() |