We have a Redshift Spectrum table built on top of S3 data - we are trying to automate the partition addition in this table - I can run the following ALTER statement in a redshift client or psql shell:
ALTER TABLE analytics_spectrum.page_view ADD PARTITION(date='2017-10-17') LOCATION 's3://data-hub/page_view/2017/10/17/';
But this fails to execute via psycopg2.
sql_query = "ALTER TABLE analytics_spectrum.page_view ADD PARTITION(date='2017-10-17') LOCATION 's3://data-hub/_page_view_v3/2017/10/17/';"
import config
import psycopg2
connection = psycopg2.connect(
**config.DATABASES['redshift_db']["connection"])
cursor = connection.cursor()
cursor.execute(sql_query)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
psycopg2.ProgrammingError: syntax error at or near "("
LINE 1: ...ABLE analytics_spectrum.page_view ADD PARTITION(date='201...
In case of psycopg2 it doesn't even send the query to redshift and execution fail in query parsing.
For now I have implemented using subprocess.popen to execute alter statement - but I would like to switch it back to use psycopg2.
p = subprocess.Popen(['psql',
'-h', self.spectrum_connection['host'],
'-p', self.spectrum_connection['port'],
'-d', self.spectrum_connection['dbname'],
'-U', self.spectrum_connection['user'],
'-c', sql_stmt],
env={
'PGPASSWORD': self.spectrum_connection['password']},
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate()
suggestions /thoughts ?
Thanks, Hussain Bohra