헤르메스 LIFE

[Python] DB Connection - Postgresql 본문

Python

[Python] DB Connection - Postgresql

헤르메스의날개 2021. 12. 18. 20:13
728x90

https://hermeslog.tistory.com/541?category=302344 

 

[SpringBoot] PostgreSQL 연결하기

IDE : IntelliJ JDK : OpenJDK 11 Framework : Spring Boot 2.5.2 Database : PostgreSQL 최신버전 ( 라이센스도 소스공개의무도 없음 ) 첨부파일이 MySqlRunner 로 되어있는데.. MySQL 접속테스트 중 소스만 바..

hermeslog.tistory.com


import psycopg2 as psycopg2

conn = None
try:
    # read connection parameters

    # connect to the PostgreSQL server
    print('Connecting to the PostgreSQL database...')
    conn = psycopg2.connect("dbname=springboot user=hermeswing password=pass")

    # create a cursor
    cur = conn.cursor()

    # execute a statement
    print('PostgreSQL database version:')
    cur.execute('SELECT version()')

    # display the PostgreSQL database server version
    db_version = cur.fetchone()
    print(db_version)

    # close the communication with the PostgreSQL
    cur.close()
except (Exception, psycopg2.DatabaseError) as error:
    print(error)
finally:
    if conn is not None:
        conn.close()
        print('Database connection closed.')
728x90