{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Database and table creation completed successfully!\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sqlalchemy import create_engine\n",
    "import psycopg2\n",
    "\n",
    "# Step 1: Load the Excel file into a DataFrame\n",
    "file_path = '/home/dadams/Repos/colorado_spills/Spills.xlsx'\n",
    "df = pd.read_excel(file_path)\n",
    "\n",
    "\n",
    "# Step 2: Create PostgreSQL database and enable PostGIS\n",
    "db_name = 'colorado_spills'\n",
    "user = 'postgres'\n",
    "password = 'MandyLinkToby3'\n",
    "host = '192.168.0.74'\n",
    "port = '5432'\n",
    "\n",
    "# Create a connection to PostgreSQL\n",
    "engine = create_engine(f'postgresql+psycopg2://{user}:{password}@{host}:{port}/postgres')\n",
    "\n",
    "# Connect to PostgreSQL\n",
    "conn = psycopg2.connect(dbname='postgres', user=user, password=password, host=host, port=port)\n",
    "conn.autocommit = True\n",
    "cur = conn.cursor()\n",
    "\n",
    "# Create database\n",
    "cur.execute(f'CREATE DATABASE {db_name}')\n",
    "cur.close()\n",
    "conn.close()\n",
    "\n",
    "# Connect to the new database\n",
    "engine = create_engine(f'postgresql+psycopg2://{user}:{password}@{host}:{port}/{db_name}')\n",
    "conn = psycopg2.connect(dbname=db_name, user=user, password=password, host=host, port=port)\n",
    "conn.autocommit = True\n",
    "cur = conn.cursor()\n",
    "\n",
    "# Enable PostGIS extension\n",
    "cur.execute('CREATE EXTENSION postgis')\n",
    "cur.close()\n",
    "conn.close()\n",
    "\n",
    "# Step 3: Load DataFrame into PostgreSQL table\n",
    "table_name = 'spills'\n",
    "df.to_sql(table_name, engine, if_exists='replace', index=False)\n",
    "\n",
    "print(\"Database and table creation completed successfully!\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "funkyfunk",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
