question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Execute a psql script within a Python script

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
andr3w321commented, Feb 11, 2017

Yep that should work. Remember to close the cursor and connection when you’re done with

cur.close()
conn.close()

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 become conn = psycopg2.connect("host=localhost dbname=nfldb user=nfldb") You can query with

week = 5
cur.execute("""SELECT * FROM game WHERE week = %s LIMIT 5;""",(week,))
print cur.fetchall()
0reactions
mrg1999commented, Feb 16, 2017

@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 the pg_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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found