User Tools

Site Tools


pythonpostgresql

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
pythonpostgresql [2021/11/29 21:55] z0hpvkpythonpostgresql [2025/03/08 22:24] (current) – external edit 127.0.0.1
Line 26: Line 26:
 </code> </code>
  
 +=== Example Connection Files ===
 +== Using Config Parser ==
 <code python> <code python>
 # database.ini # database.ini
Line 34: Line 36:
 password=pwd password=pwd
 </code> </code>
- 
 <code python> <code python>
 #!/usr/bin/python #!/usr/bin/python
Line 66: Line 67:
     print(error)     print(error)
 </code> </code>
 +
 +== Using Credential File ==
 +<code python>
 +# conndetails.py
 +PGHOST="localhost"
 +PGDATABASE="doob"
 +PGUSER="ian"
 +PGPASSWORD="pwd"
 +</code>
 +
 +<code python>
 +import psycopg2
 +import conndetails as creds
 +
 +conn_string = "host="+ creds.PGHOST +" dbname="+ creds.PGDATABASE +" user=" 
 +               + creds.PGUSER +" password="+ creds.PGPASSWORD
 +conn = psycopg2.connect(conn_string)
 +
 +cur = conn.cursor()
 +query = "select version()"
 +cur.execute(query)
 +for row in cur:
 +  print(row)
 +
 +cur.close() 
 +</code>
 +
 +
          
  
pythonpostgresql.1638222911.txt.gz · Last modified: 2025/03/08 22:23 (external edit)