Skip to content
Advertisement

Rename a redshift SQL table within PySpark Databricks

I want to rename a redshift table within a Python Databricks notebook.

Currently I have a query that pulls in data and creates a table:

JavaScript

I want to take this table I created and rename it. I referenced this doc but find it hard to follow.

I want to run this SQL command alter table public.test rename to test_table_to_be_dropped in my pyspark databricks notebook. (this command works within my SQL IDE)

Here’s what I have tried:

JavaScript

This gives me an error:

JavaScript

It leads me to believe that my run_query() function is only for reading data and not for editing data. I also saw that I can use the %sql magic command, but it looks like it will only reference my data lake and not my redshift database.

TL;DR: Is there a way I can write a function to use sql write commands or to point my %sql magic command to reference my redshift table?

I want to run this SQL command alter table public.test rename to test_table_to_be_dropped in my pyspark databricks notebook. (this command works within my SQL IDE)

Advertisement

Answer

AWS Redshift has a postactions and preactions parameter that allows you to write SQL queries after your write query.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement