Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute .sql schema in psycopg2 in Python

I have a PostgreSQL schema stored in .sql file. It looks something like:

CREATE TABLE IF NOT EXISTS users (     id INTEGER PRIMARY KEY,     facebook_id TEXT NOT NULL,     name TEXT NOT NULL,     access_token TEXT,     created INTEGER NOT NULL ); 

How shall I run this schema after connecting to the database?

My existing Python code works for SQLite databases:

# Create database connection self.connection = sqlite3.connect("example.db")  # Run database schema with self.connection as cursor:     cursor.executescript(open("schema.sql", "r").read()) 

But the psycopg2 doesn't have an executescript method on the cursor. So, how can I achieve this?

like image 271
linkyndy Avatar asked Jun 23 '13 13:06

linkyndy


People also ask

How do I specify schema in psycopg2 connection method?

When you execute a query using the cursor from that connection, it will search across those schemas mentioned in options i.e., dbo,public in sequence from left to right. Hope this might help you. Show activity on this post. This helps if you are using SQLAlchemy for example.

How do I run a .SQL file in Python?

sql file within a single cursor object in Pythonlink. To execute the entire file, we must first open it and read the contents into the cursor. execute() method, however this would not work without the additional use of the 'multi=True' argument which allows for multiple statements to be provided to the method.

How do I run a SQL script in PostgreSQL?

Another easiest and most used way to run any SQL file in PostgreSQL is via its SQL shell. Open the SQL shell from the menu bar of Windows 10. Add your server name, database name where you want to import the file, the port number you are currently active on, PostgreSQL username, and password to start using SQL shell.


1 Answers

You can just use execute:

with self.connection as cursor:     cursor.execute(open("schema.sql", "r").read()) 

though you may want to set psycopg2 to autocommit mode first so you can use the script's own transaction management.

It'd be nice if psycopg2 offered a smarter mode where it read the file in a statement-at-a-time and sent it to the DB, but at present there's no such mode as far as I know. It'd need a fairly solid parser to do it correctly when faced with $$ quoting (and its $delimiter$ variant where the deimiter may be any identifier), standard_conforming_strings, E'' strings, nested function bodies, etc.

Note that this will not work with:

  • anything containing psql backslash commands
  • COPY .. FROM STDIN
  • very long input

... and therefore won't work with dumps from pg_dump

like image 172
Craig Ringer Avatar answered Oct 11 '22 19:10

Craig Ringer