|
| 1 | +--- |
| 2 | +title: Create a container in Azure Cosmos DB for NoSQL using Python |
| 3 | +description: Learn how to create a container in your Azure Cosmos DB for NoSQL database using the Python SDK. |
| 4 | +author: seesharprun |
| 5 | +ms.author: sidandrews |
| 6 | +ms.service: cosmos-db |
| 7 | +ms.subservice: nosql |
| 8 | +ms.devlang: python |
| 9 | +ms.topic: how-to |
| 10 | +ms.date: 12/06/2022 |
| 11 | +ms.custom: devx-track-python, devguide-python, cosmos-db-dev-journey |
| 12 | +--- |
| 13 | + |
| 14 | +# Create a container in Azure Cosmos DB for NoSQL using Python |
| 15 | + |
| 16 | +[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)] |
| 17 | + |
| 18 | +Containers in Azure Cosmos DB store sets of items. Before you can create, query, or manage items, you must first create a container. |
| 19 | + |
| 20 | +## Name a container |
| 21 | + |
| 22 | +In Azure Cosmos DB, a container is analogous to a table in a relational database. When you create a container, the container name forms a segment of the URI used to access the container resource and any child items. |
| 23 | + |
| 24 | +Here are some quick rules when naming a container: |
| 25 | + |
| 26 | +- Keep container names between 3 and 63 characters long |
| 27 | +- Container names can only contain lowercase letters, numbers, or the dash (-) character. |
| 28 | +- Container names must start with a lowercase letter or number. |
| 29 | + |
| 30 | +Once created, the URI for a container is in this format: |
| 31 | + |
| 32 | +`https://<cosmos-account-name>.documents.azure.com/dbs/<database-name>/colls/<container-name>` |
| 33 | + |
| 34 | +## Create a container |
| 35 | + |
| 36 | +To create a container, call one of the following methods: |
| 37 | + |
| 38 | +- [CreateContainerAsync](#create-a-container) |
| 39 | +- [CreateContainerIfNotExistsAsync](#create-a-container-if-it-doesnt-already-exist) |
| 40 | +- [Create a container asynchronously](#create-a-container-asynchronously) |
| 41 | + |
| 42 | +### Create a container |
| 43 | + |
| 44 | +The following example creates a container with the [``DatabaseProxy.create_container``](/python/api/azure-cosmos/azure.cosmos.databaseproxy#azure-cosmos-databaseproxy-create-container) method. This method throws an exception if the container with the same name already exists. |
| 45 | + |
| 46 | +:::code language="python" source="~/cosmos-db-nosql-python-samples/005-create-container/app.py" id="create_container"::: |
| 47 | + |
| 48 | +### Create a container if it doesn't already exist |
| 49 | + |
| 50 | +The following example creates a container with the [``DatabaseProxy.create_container_if_not_exists``](/python/api/azure-cosmos/azure.cosmos.databaseproxy#azure-cosmos-databaseproxy-create-container-if-not-exist) method. Compared to the previous create method, this method doesn't throw an exception if the database already exists. This method is useful for avoiding errors if you run the same code multiple times. |
| 51 | + |
| 52 | +:::code language="python" source="~/cosmos-db-nosql-python-samples/005-create-container/app_exists.py" id="create_container"::: |
| 53 | + |
| 54 | +### Create a container asynchronously |
| 55 | + |
| 56 | +You can also create a database asynchronously using similar object and methods in the [azure.cosmos.aio](/python/api/azure-cosmos/azure.cosmos.aio) namespace. For example, use the [`DatabaseProxy.create_database`](/python/api/azure-cosmos/azure.cosmos.aio.databaseproxy#azure-cosmos-aio-databaseproxy-create-container) method or the ['CosmoClient.create_database_if_not_exists](/python/api/azure-cosmos/azure.cosmos.aio.databaseproxy#azure-cosmos-aio-databaseproxy-create-container-if-not-exists) method. |
| 57 | + |
| 58 | +Working asynchronously is useful when you want to perform multiple operations in parallel. For more information, see [Using the asynchronous client](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/cosmos/azure-cosmos#using-the-asynchronous-client). |
| 59 | + |
| 60 | +## Parsing the response |
| 61 | + |
| 62 | +In the examples above, the response from the requests is a [``ContainerProxy``](/python/api/azure-cosmos/azure.cosmos.containerproxy), which is an interface to interact with a DB Container. From the proxy, you can access methods to perform operations on the container. |
| 63 | + |
| 64 | +The following example shows the **create_container_if_not_exists** method returning a **container** object. |
| 65 | + |
| 66 | +:::code language="python" source="~/cosmos-db-nosql-python-samples/005-create-container/app_exists.py" id="parse_response"::: |
| 67 | + |
| 68 | +## Next steps |
| 69 | + |
| 70 | +Now that you've create a container, use the next guide to create items. |
| 71 | + |
| 72 | +> [!div class="nextstepaction"] |
| 73 | +> [Examples for Azure Cosmos DB for NoSQL SDK for Python](samples-python.md) |
0 commit comments