Commit a1d5b733 authored by Johannes Bleher's avatar Johannes Bleher
Browse files

Python scripts added

parent 0ad7265c
Loading
Loading
Loading
Loading
+137 −0
Original line number Diff line number Diff line
%% Cell type:markdown id:e7a51a6a-6eae-4af9-a2b7-e9a0816b3696 tags:

# Update conda & Set mamba as solver & Set up psycopg2

%% Cell type:code id:f307fbbd-8e4c-443e-862b-a75a1685a29a tags:

``` python
!conda update -n base -y conda
!conda install -n base -y conda-libmamba-solver
!conda config --set solver libmamba
!conda install -y psycopg2
```

%% Output

    Channels:
     - conda-forge
     - defaults
    Platform: linux-64
    Collecting package metadata (repodata.json): done
    Solving environment: done
    
    
    ==> WARNING: A newer version of conda exists. <==
        current version: 23.9.0
        latest version: 23.10.0
    
    Please update conda by running
    
        $ conda update -n base -c conda-forge conda
    
    
    
    ## Package Plan ##
    
      environment location: /opt/conda
    
      added / updated specs:
        - conda
    
    
    The following packages will be downloaded:
    
        package                    |            build
        ---------------------------|-----------------
        pexpect-4.8.0              |     pyh9f0ad1d_2          47 KB  conda-forge
        xorg-xproto-7.0.31         |    h14c3975_1007          72 KB  conda-forge
        ------------------------------------------------------------
                                               Total:         119 KB
    
    The following packages will be DOWNGRADED:
    
      fribidi                                 1.0.10-h516909a_0 --> 1.0.10-h36c2ea0_0
      pexpect                                4.8.0-pyh1a96a4e_2 --> 4.8.0-pyh9f0ad1d_2
      xorg-inputproto                       2.3.2-h14c3975_1002 --> 2.3.2-h7f98852_1002
      xorg-xproto                          7.0.31-h7f98852_1007 --> 7.0.31-h14c3975_1007
    
    
    
    Downloading and Extracting Packages:
    pexpect-4.8.0        | 47 KB     |                                       |   0%
    pexpect-4.8.0        | 47 KB     | ##################################### | 100% [A
    xorg-xproto-7.0.31   | 72 KB     | ##################################### | 100% [A
                                                                                    [A
                                                                                    [A
    Preparing transaction: done
    Verifying transaction: done
    Executing transaction: done
    Channels:
     - conda-forge
     - defaults
    Platform: linux-64
    Collecting package metadata (repodata.json): done
    Solving environment: done
    
    
    ==> WARNING: A newer version of conda exists. <==
        current version: 23.9.0
        latest version: 23.10.0
    
    Please update conda by running
    
        $ conda update -n base -c conda-forge conda
    
    

%% Cell type:markdown id:39a658a0-7669-41cd-a56a-265bad47e891 tags:

# Connect to database

%% Cell type:code id:4e31c9fd-130a-44e5-b788-6ffee4cbfb8f tags:

``` python
# Preamble
import psycopg2
import pandas as pd

# Task: Database Connection

# Database credentials
dsn_database = "aidaho"  # Specify the name of your Database
dsn_hostname = "193.196.53.49"  # localhost = 127.0.0.1
dsn_port = 8001  # Specify your port number, e.g., 98939
dsn_uid = "student"  # Specify your username, e.g., "admin"
dsn_pwd = "aidaho"  # Specify your password, e.g., "xxx"

# Connect to the database
try:
    connect = psycopg2.connect(
        dbname=dsn_database,
        user=dsn_uid,
        password=dsn_pwd,
        host=dsn_hostname,
        port=dsn_port
    )
    print("Database Connected!")

    # Check Connection
    cursor = connect.cursor()
    cursor.execute("SELECT version();")
    record = cursor.fetchone()
    print("You are connected to - ", record, "\n")

except (Exception, psycopg2.Error) as error:
    print("Error while connecting to PostgreSQL", error)
```

%% Output

    Database Connected!
    You are connected to -  ('PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit',)
    

%% Cell type:code id:7943e919-8504-4b90-a1a2-580c3889a486 tags:

``` python
```
+44 −0
Original line number Diff line number Diff line
# Preamble
import psycopg2
from psycopg2 import sql
import pandas as pd

# Task: Database Connection

# Database credentials
dsn_database = "aidaho"  # Specify the name of your Database
dsn_hostname = "193.196.53.49"  # localhost = 127.0.0.1
dsn_port = 8001  # Specify your port number, e.g., 98939
dsn_uid = "student"  # Specify your username, e.g., "admin"
dsn_pwd = "aidaho"  # Specify your password, e.g., "xxx"

# Connect to the database
try:
    connect = psycopg2.connect(
        dbname=dsn_database,
        user=dsn_uid,
        password=dsn_pwd,
        host=dsn_hostname,
        port=dsn_port
    )
    print("Database Connected!")

    # Check Connection
    cursor = connect.cursor()
    cursor.execute("SELECT version();")
    record = cursor.fetchone()
    print("You are connected to - ", record, "\n")

except (Exception, psycopg2.Error) as error:
    print("Error while connecting to PostgreSQL", error)






# Closing database connection.
if connect:
    cursor.close()
    connect.close()
    print("PostgreSQL connection is closed")