Creating database objects
You can create database objects, including databases, schemas, tables, and user-defined functions (UDFs). You must be connected to a cluster or workgroup and a database to create database objects.
Creating databases
You can use query editor v2 to create databases in your cluster or workgroup.
To create a database
For information about databases, see CREATE DATABASE in the Amazon Redshift Database Developer Guide.
-
Choose Create, and then choose Database.
-
Enter a Database name.
-
(Optional) Select Users and groups, and choose a Database user.
-
(Optional) You can create the database from a datashare or the Amazon Glue Data Catalog. For more information about Amazon Glue, see What is Amazon Glue? in the Amazon Glue Developer Guide.
-
(Optional) Select Create using a datashare, and choose a Select a datashare. The list includes producer datashares that can be used to create a consumer datashare in the current cluster or workgroup.
-
(Optional) Select Create using Amazon Glue Data Catalog, and choose a Choose an Amazon Glue database. In Data catalog schema, enter the name that will be used for the schema when referencing the data in a three-part name (database.schema.table).
-
-
Choose Create database.
The new database displays in the tree-view panel.
When you choose the optional step to query a database created from a datashare, connect to a Amazon Redshift database in the cluster or workgroup (for example, the default database
dev
), and use three-part notation (database.schema.table) that references the database name you created when you selected Create using a datashare. The datasharing database is listed in the query editor v2 editor tab, but it is not enabled for direct connection.When you choose the optional step to query a database created from a Amazon Glue Data Catalog, connect to your Amazon Redshift database in the cluster or workgroup (for example, the default database
dev
), and use three-part notation (database.schema.table) that references the database name you created when you selected Create using Amazon Glue Data Catalog, the schema you named in Data catalog schema, and the table in the Amazon Glue Data Catalog. Similar to:SELECT * FROM
glue-database
.glue-schema
.glue-table
Note
Confirm that you are connected to the default database using the connection method Temporary credentials using your IAM identity, and that your IAM credentials have been granted usage privilege to the Amazon Glue database.
GRANT USAGE ON DATABASE
glue-database
to "IAM:MyIAMUser"The Amazon Glue database is listed in the query editor v2 editor tab, but it is not enabled for direct connection.
For more information about querying an Amazon Glue Data Catalog, see Working with Lake Formation-managed datashares as a consumer and Working with Lake Formation-managed datashares as a producer in the Amazon Redshift Database Developer Guide.
Example creating a database as a datashare consumer
The following example describes a specific scenario that was used to create a
database from a datashare using query editor v2. Review this scenario to learn how you
can create a database from a datashare in your environment. This scenario uses
two clusters, cluster-base
(the producer cluster) and
cluster-view
(the consumer cluster).
-
Use the Amazon Redshift console to create a datashare for the table
category2
in clustercluster-base
. The producer datashare is nameddatashare_base
.For information about creating datashares, see Sharing data across clusters in Amazon Redshift in the Amazon Redshift Database Developer Guide.
-
Use the Amazon Redshift console to accept the datashare
datashare_base
as a consumer for the tablecategory2
in clustercluster-view
. -
View the tree-view panel in query editor v2 which shows the hierarchy of
cluster-base
as:-
Cluster:
cluster-base
-
Database:
dev
-
Schema:
public
-
Tables:
category2
-
-
-
-
-
Choose Create, and then choose Database.
-
Enter
see_datashare_base
for Database name. -
Select Create using a datashare, and choose a Select a datashare. Choose
datashare_base
to use as the source of the database you are creating.The tree-view panel in query editor v2 shows the hierarchy of
cluster-view
as:-
Cluster:
cluster-view
-
Database:
see_datashare_base
-
Schema:
public
-
Tables:
category2
-
-
-
-
-
When you query the data, connect to the default database of the cluster
cluster-view
(typically nameddev
), but reference the datashare databasesee_datashare_base
in your SQL.Note
In the query editor v2 editor view, the selected cluster is
cluster-view
. The selected database isdev
. The databasesee_datashare_base
is listed but is not enabled for direct connection. You choose thedev
database and referencesee_datashare_base
in the SQL you run.SELECT * FROM "see_datashare_base"."public"."category2";
The query retrieves data from the datashare
datashare_base
in the clustercluster_base
.
Example creating a database from an Amazon Glue Data Catalog
The following example describes a specific scenario that was used to create a
database from an Amazon Glue Data Catalog using query editor v2. Review this scenario to learn how you
can create a database from an Amazon Glue Data Catalog in your environment. This scenario
uses one cluster, cluster-view
to contain the database you
create.
-
Choose Create, and then choose Database.
-
Enter
data_catalog_database
for Database name. -
Select Create using a Amazon Glue Data Catalog, and choose Choose an Amazon Glue database. Choose
glue_db
to use as the source of the database you are creating.Choose Data catalog schema and enter
myschema
as the schema name to use in three-part notation.The tree-view panel in query editor v2 shows the hierarchy of
cluster-view
as:-
Cluster:
cluster-view
-
Database:
data_catalog_database
-
Schema:
myschema
-
Tables:
category3
-
-
-
-
-
When you query the data, connect to the default database of the cluster
cluster-view
(typically nameddev
), but reference the databasedata_catalog_database
in your SQL.Note
In the query editor v2 editor view, the selected cluster is
cluster-view
. The selected database isdev
. The databasedata_catalog_database
is listed but is not enabled for direct connection. You choose thedev
database and referencedata_catalog_database
in the SQL you run.SELECT * FROM "data_catalog_database"."myschema"."category3";
The query retrieves data that is cataloged by Amazon Glue Data Catalog.
Creating schemas
You can use query editor v2 to create schemas in your cluster or workgroup.
To create a schema
For information about schemas, see Schemas in the Amazon Redshift Database Developer Guide.
-
Choose Create, and then choose Schema.
-
Enter a Schema name.
-
Choose either Local or External as the Schema type.
For more information about local schemas, see CREATE SCHEMA in the Amazon Redshift Database Developer Guide. For more information about external schemas, see CREATE EXTERNAL SCHEMA in the Amazon Redshift Database Developer Guide.
-
If you choose External, then you have the following choices of an external schema.
-
Glue Data Catalog – to create an external schema in Amazon Redshift that references tables in Amazon Glue. Besides choosing the Amazon Glue database, choose the IAM role associated with the cluster and the IAM role associated with the Data Catalog.
-
PostgreSQL – to create an external schema in Amazon Redshift that references an Amazon RDS for PostgreSQL or Amazon Aurora PostgreSQL-Compatible Edition database. Also provide the connection information to the database. For more information about federated queries, see Querying data with federated queries in the Amazon Redshift Database Developer Guide.
-
MySQL – to create an external schema in Amazon Redshift that references an Amazon RDS for MySQL or and Amazon Aurora MySQL-Compatible Edition database. Also provide the connection information to the database. For more information about federated queries, see Querying data with federated queries in the Amazon Redshift Database Developer Guide.
-
-
Choose Create schema.
The new schema appears in the tree-view panel.
Creating tables
You can use query editor v2 to create tables in your cluster or workgroup.
To create a table
You can create a table based on a comma-separated value (CSV) file that you specify or define each column of the table. For information about tables, see Designing tables and CREATE TABLE in the Amazon Redshift Database Developer Guide.
Choose Open query in editor to view and edit the CREATE TABLE statement before you run the query to create the table.
-
Choose Create, and choose Table.
-
Choose a schema.
-
Enter a table name.
-
Choose Add field to add a column.
-
Use a CSV file as a template for the table definition:
-
Choose Load from CSV.
-
Browse to the file location.
If you use a CSV file, be sure that the first row of the file contains the column headings.
-
Choose the file and choose Open. Confirm that the column names and data types are what you intend.
-
-
For each column, choose the column and choose the options that you want:
-
Choose a value for Encoding.
-
Choose a Default value.
-
Turn on Automatically increment if you want the column values to increment. Then specify a value for Auto increment seed and Auto increment step.
-
Turn on Not NULL if the column should always contain a value.
-
Enter a Size value for the column.
-
Turn on Primary key if you want the column to be a primary key.
-
Turn on Unique key if you want the column to be a unique key.
-
-
(Optional) Choose Table details and choose any of the following options:
-
Distribution key column and style.
-
Sort key column and sort type.
-
Turn on Backup to include the table in snapshots.
-
Turn on Temporary table to create the table as a temporary table.
-
-
Choose Open query in editor to continue specifying options to define the table or choose Create table to create the table.
Creating functions
You can use query editor v2 to create functions in your cluster or workgroup.
To create a function
-
Choose Create, and choose Function.
-
For Type, choose SQL or Python.
-
Choose a value for Schema.
-
Enter a value for Name for the function.
-
Enter a value for Volatility for the function.
-
Choose Parameters by their data types in the order of the input parameters.
-
For Returns, choose a data type.
-
Enter the SQL program or Python program code for the function.
-
Choose Create.
For more information about user-defined functions (UDFs), see Creating user-defined functions in the Amazon Redshift Database Developer Guide.