SQL
Python에서 PostgreSQL 연결을 위한 설정
heed159
2022. 5. 20. 17:32
1. postgresql.conf 설정
postgreSQL에 대한 클라이언트의 연결을 허용할 호스트와 IP 주소를 설정한다.
listen_addressed = '*'
port = 35432
2. pg_hba.conf 설정
클라이언트의 주소와 역할 이름을 지정하고 모든 데이터베이스에 연결을 허용할지 여부를 설정한다.
TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 0.0.0.0/0 md5
**127.0.0.1/32**는 로컬 루프백 주소라고 하는 주소에서 자신을 나타내는 IP 주소이다.
이 경우에는 PostgreSQL이 실행 중인 서버의 IP주소를 말하고 localhost와도 같은 의미이다.
3. postgreSQL 포트 연결(docker > local)
nc -vz 192.168.0.9 35432
도커에서 로컬 IP 주소 / 포트로 연결
4. python conn 생성
try:
conn = psycopg2.connect(host='192.168.0.9', dbname='postgres', user='postgres', password="1234", port=35432)
cur=conn.cursor()
#cur.execute("select st_asewkt(st_geomfromgml('{0}'));".format(str1))
cur.execute("select version()")
print(cur.fetchone())
except:
print("Not Connected!")