pythonpostgresql
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
pythonpostgresql [2021/11/29 21:55] – z0hpvk | pythonpostgresql [2025/03/08 22:24] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 26: | Line 26: | ||
</ | </ | ||
+ | === Example Connection Files === | ||
+ | == Using Config Parser == | ||
<code python> | <code python> | ||
# database.ini | # database.ini | ||
Line 34: | Line 36: | ||
password=pwd | password=pwd | ||
</ | </ | ||
- | |||
<code python> | <code python> | ||
# | # | ||
Line 66: | Line 67: | ||
print(error) | print(error) | ||
</ | </ | ||
+ | |||
+ | == Using Credential File == | ||
+ | <code python> | ||
+ | # conndetails.py | ||
+ | PGHOST=" | ||
+ | PGDATABASE=" | ||
+ | PGUSER=" | ||
+ | PGPASSWORD=" | ||
+ | </ | ||
+ | |||
+ | <code python> | ||
+ | import psycopg2 | ||
+ | import conndetails as creds | ||
+ | |||
+ | conn_string = " | ||
+ | + creds.PGUSER +" password=" | ||
+ | conn = psycopg2.connect(conn_string) | ||
+ | |||
+ | cur = conn.cursor() | ||
+ | query = " | ||
+ | cur.execute(query) | ||
+ | for row in cur: | ||
+ | print(row) | ||
+ | |||
+ | cur.close() | ||
+ | </ | ||
+ | |||
+ | |||
| | ||
pythonpostgresql.1638222911.txt.gz · Last modified: 2025/03/08 22:23 (external edit)