By | 12 Januari 2022
  1. Install paket psycopg2
    [email protected]:~
    $ conda activate
    (base) [email protected]:~
    $ conda install -c anaconda psycopg2


  2. Buat script untuk akses ke PostgreSQL
    import psycopg2

    # Establishing the connection.
    conn = psycopg2.connect(
    database="postgres", user='postgres', password='password', host='localhost', port= '5432'
    )

    # Creating a cursor object using the cursor() method.
    cursor = conn.cursor()

    Executing an MYSQL function using the execute() method.
    cursor.execute("select version()")

    # Fetch a single row using fetchone() method.
    data = cursor.fetchone()
    print("Connection established to: ",data)

  3. Contoh output script di Jupyter Notebook
    Connection established to: ('PostgreSQL 9.4.5 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 8.3.1 20190507 (Red Hat 8.3.1-4), 64-bit',)

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan.