Execute a psql script within a Python script
See original GitHub issueI would like to Insert JAX into the ‘team’ table inside the nfldb-update script
i.e.
psql -U nfldb -f C:\Python27\Insert_JAX.sql
where Insert_JAX.sql is
INSERT into team values('JAX','Jacksonville', 'Jaguars')
Is this possible? And could someone help me with the correct syntax to do this?
Issue Analytics
- State:
- Created 7 years ago
- Comments:8
Top Results From Across the Web
Python Script to Execute SQL Statements on PostgreSQL
To access PostgreSQL from a Python scripts you need to have psycopg2 module. It is a database adopter for PostgreSQL. First you need...
Read more >How to execute a python script form a stored procedure in ...
CREATE FUNCTION callMyApp() RETURNS VOID AS $$ import subprocess subprocess.call(['/usr/bin/python', '/path/to/MyApp']) $$ LANGUAGE plpythonu; ...
Read more >How To: Connect and run SQL queries to a PostgreSQL ...
Import the module in the Python script: import psycopg2 · Make a connection to a PostgreSQL database by passing in the appropriate user/password...
Read more >Executing SQL query with Psycopg2 in Python - GeeksforGeeks
Psycopg2 is a PostgreSQL database driver, it is used to perform operations on PostgreSQL using python, it is designed for multi-threaded ...
Read more >[Solved]-Execute psql command from python script-postgresql
[Solved]-Execute psql command from python script-postgresql ... It seems like you are trying to run a Meta-Command through psycopg2 similar to to this...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yep that should work. Remember to close the cursor and connection when you’re done with
I would also recommend moving the password into a
.pgpass
file(google it for configuration) for easier access and more security so you don’t litter your scripts with your db pass. Your connection string will then becomeconn = psycopg2.connect("host=localhost dbname=nfldb user=nfldb")
You can query with@andr3w321
update.py
seems to be closing the cursor and connection on its own. I get an error that they are already closed so I left it out. My authentication method is set to trust in thepg_hba.conf
file so no password is needed. Currently my program downloads the stats of every game in progress every 10 seconds but I think a more efficient way would be to monitor ‘some_file’ for a change in the modified time, indicating a change in a game in progress (score, down, end of quarter, etc.) Just not sure which file would be the best one to monitor.