create view in redshift particular schema - sql

I've created an external schema in my redshift database using the script below:
create external schema exampleschema
from data catalog database 'examplesource'
iam_role 'arn:aws:iam::627xxxxx:role/dxxxx'
region 'us-west-2'
CREATE EXTERNAL DATABASE IF NOT EXISTS;
I'm now trying to create a view in that exampleschema schema using the script below, but I seem to only be able to create views in the "public" schema. How do I create a view in the exampleschema schema?
create view vw_ticket as select * from exampleschema.ticket
with no schema binding;

You need to specify the schema in the CREATE statement i.e. create view exampleschema.vw_ticket ...

Related

How to alter external schema IAM Role in Redshift?

Based on: https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_EXTERNAL_SCHEMA.html
I have my schema declared in the following way:
create external schema spectrum_schema
from data catalog
database 'spectrum_db'
iam_role 'arn:aws:iam::123456789012:role/myRedshiftRole,arn:aws:iam::123456789012:role/myS3Role'
catalog_role 'arn:aws:iam::123456789012:role/myAthenaRole'
create external database if not exists;
I decided I want to change my IAM Role for this schema and having something like arn:aws:iam::123456789012:role/moreBeautifulRole instead.
Is it possible to change it? Ideally I'd like to change it with something like
ALTER SCHEMA spectrum_schema IAM_ROLE 'arn:aws:iam::123456789012:role/moreBeautifulRole'
without destroying the schema. Please share the available options.
After confirming with AWS Support, at the date posting this answer it is not supported to edit the IAM Role in an existing External Schema.
Available options I can think are:
Create a new External Schema with the moreBeautifulRole
Add Policy to myS3Role with the required permission - or edit the already assigned one
Drop and recreate external schema and depending objects (including external tables). Using DBT is not too bad as approach

What happens if I don´t define a specific schema to the database?

If a specific schema is not defined in a database, where are the database objects going to be stored? Is that a good or a bad thing? Why?
Quote from the manual
In the previous sections we created tables without specifying any schema names. By default such tables (and other objects) are automatically put into a schema named “public”. Every new database contains such a schema
If no schema is defined when creating a table, the first (existing) schema that is found in the schema search path will be used to store the table.
In psql
create database sch_test;
CREATE DATABASE
\c sch_test
You are now connected to database "sch_test" as user "postgres".
--Show available schemas
\dn
List of schemas
Name | Owner
--------+----------
public | postgres
drop schema public ;
DROP SCHEMA
\dn
List of schemas
Name | Owner
------+-------
(0 rows)
show search_path ;
search_path
-----------------
"$user", public
create table tbl(id integer);
ERROR: no schema has been selected to create in
LINE 1: create table tbl(id integer);
create table test.tbl(id integer);
ERROR: schema "test" does not exist
LINE 1: create table test.tbl(id integer);
Just to show that an object may not be created if a schema does not exist. Bottom line is an object(table, function, etc) needs to be created in a schema. If there is none available for search_path to find or you specifically point at one that does not exist the object creation will fail.

Can I alias an external table in redshift to remove the schema name?

We want to migrate tables to Spectrum, which requires defining an external schema
create external schema spectrum
from data catalog
database 'spectrumdb'
iam_role 'my_iam_role'
create external database if not exists;
I created an external table in Redshift like this:
create external table spectrum.my_table(
id bigint,
accountId bigint,
state varchar(65535),
) stored as parquet
location 's3://some_bucket/my_table_files';
Is it possible to alias the table such that when querying it, I can call it my_table_alias instead of spectrum.my_table? Basically, we want to make the change to external tables opaque to clients of our Redshift instance (this means we can't change the table names). Thanks so much for your help!
Redshift does not have aliases, your best option is to create a view.
You need to use WITH NO SCHEMA BINDING option while creating the view since the view is on an external table.
If you like to not specify schema names or you have a requirement like this create the view(s) in public schema or set the users default schema to the schema where the views are
alter user .. set search_path to ..
Additional benefits of using a view to access an external table are, you can
rename columns to be more user friendly
add or remove columns with view definition
change data types and/or date/time formats
you will have the ability to change name/structure of the external table without effecting user access
Let me know if this answers your question.

Db2 drop schema contents along with schema at once

Is there a query in db2 9.7 control center wherein I can't DELETE(DROP) all the contents of my schema (including the schema) at once?
My other option is to drop/delete the objects first and then DROP schema..
But I want to DROP THE ENTIRE SCHEMA WITH ALL OBJECTS at once.
DROP SCHEMA <schema_name> CASCADE/RESTRICT didn't work for me.
The ADMIN_DROP_SCHEMA procedure is what you're looking for.
The ADMIN_DROP_SCHEMA procedure is used to drop a specific schema and all objects contained in it.
http://publib.boulder.ibm.com/infocenter/db2luw/v9/topic/com.ibm.db2.udb.admin.doc/doc/r0022036.htm
First drop all the tables in the schema.
Then try to delete the schema using
DROP SCHEMA SCHEMA_NAME RESTRICT
webchain.in have sample java program, explains how to delete the schema using java program
in case drop schema fails after dropping all the tables with the error SQLCODE=-551, SQLSTATE=42501, try command
grant dbadm on database to USER_NAME

PostgreSQL: Create schema in specific database

I need to write an sql script that creates both a new database AND a new schema in the database I just created.
How can I do it? Can I somehow change the current database to the new one? Or can I somehow specify the database for CREATE SCHEMA?
I'm using PostgreSQL 9.0
You can connect to the database, and execute the "CREATE SCHEMA" statement. That should result in a new schema in that database. It's not as tough as you think ;) When you want to do this from a .SQL file instead, you can use the \connect command as such:
CREATE DATABASE foo;
\connect foo;
CREATE SCHEMA yourschema;
Login to New-Database with new user:
postgres=> \connect newdb user1
...
You are now connected to database "newdb" as user "user1".
newdb=>
To create schema with new user "user1" in newdb:
newdb=> CREATE SCHEMA s1;
To list the schema :
SELECT * from information_schema.schemata;
Create database using
--CREATE DATABASE test;
Enter to the test database using
--psql -d test;
Create your schema in test database using
--create schema if not exists test_schema;