[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-27。"],[],[],null,["# Set the default time zone of a database\n\nSpanner provides\n[date](/spanner/docs/reference/standard-sql/date_functions)\nand\n[timestamp](/spanner/docs/reference/standard-sql/timestamp_functions)\nfunctions in GoogleSQL\nand Cloud SQL for PostgreSQL. Some functions, such as\n[`TIMESTAMP`](/spanner/docs/reference/standard-sql/timestamp_functions#timestamp),\nare time zone dependent and accept an optional time zone parameter. If no time\nzone parameter is provided in a function, Spanner databases\ndefault to the `America/Los_Angeles` time zone.\n\nSpanner lets you change the default time zone of a database to\ncustomize this behavior.\n\nLimitations\n-----------\n\n- You can only change the time zone of empty databases without any tables.\n- Providing a time zone parameter within a statement overrides the database's default time zone for that statement.\n- All timestamps in the [REST and RPC APIs](/spanner/docs/reference/standard-sql/data-types#for_rest_and_rpc_apis) must use UTC and end with an uppercase `Z`.\n- Timestamps in query results are consistently presented in UTC, with `Z` appended. Display time zone conversions are not performed.\n\nRequired roles\n--------------\n\n\nTo get the permissions that\nyou need to set the default time zone of a database,\n\nask your administrator to grant you the\n\n\n[Cloud Spanner Database Admin](/iam/docs/roles-permissions/spanner#spanner.databaseAdmin) (`roles/spanner.databaseAdmin`)\nIAM role on the database.\n\n\nFor more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\nThis predefined role contains\n\nthe permissions required to set the default time zone of a database. To see the exact permissions that are\nrequired, expand the **Required permissions** section:\n\n\n#### Required permissions\n\nThe following permissions are required to set the default time zone of a database:\n\n- set the default time zone of a database: ` spanner.databases.getDdl, spanner.databases.updateDdl`\n\n\nYou might also be able to get\nthese permissions\nwith [custom roles](/iam/docs/creating-custom-roles) or\nother [predefined roles](/iam/docs/roles-overview#predefined).\n\n\u003cbr /\u003e\n\nSet the default time zone\n-------------------------\n\nTo change the default time zone of your database, run the following statement: \n\n### GoogleSQL\n\nUse the\n[`ALTER DATABASE` statement](/spanner/docs/reference/standard-sql/data-definition-language#alter-database): \n\n```sql\nALTER DATABASE DATABASE-NAME SET OPTIONS (default_time_zone = '\u003cvar translate=\"no\"\u003eTIME-ZONE-NAME\u003c/var\u003e');\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eDATABASE-NAME\u003c/var\u003e: the name of the database. For example, `my-database`.\n- \u003cvar translate=\"no\"\u003eTIME-ZONE-NAME\u003c/var\u003e: the name of the time zone to set the database default to. Must be a valid entry from the [IANA Time Zone Database](https://www.iana.org/time-zones). For example, `Etc/UTC`.\n\n### PostgreSQL\n\nUse the\n[`ALTER DATABASE` statement](/spanner/docs/reference/postgresql/data-definition-language#alter-database): \n\n```sql\nALTER DATABASE DATABASE-NAME SET spanner.default_time_zone = '\u003cvar translate=\"no\"\u003eTIME-ZONE-NAME\u003c/var\u003e';\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eDATABASE-NAME\u003c/var\u003e: the name of the database. For example, `my-database`.\n- \u003cvar translate=\"no\"\u003eTIME-ZONE-NAME\u003c/var\u003e: the name of the time zone to set the database default to. Must be a valid entry from the [IANA Time Zone Database](https://www.iana.org/time-zones). For example, `Etc/UTC`.\n\nExamples\n--------\n\nThe following example queries show how to use the default time zone option.\n\n### Default time zone not customized\n\nIf the default_time_zone option is not explicitly set in the database schema,\nthen the value of default_time_zone is null and Spanner\nuses `America/Los_Angeles` as the default time zone. `America/Los_Angeles` has\nan offset of UTC-8 for timestamps in the following examples.\n\nStatement: \n\n SELECT TIMESTAMP(\"2072-12-25 15:30:00\") AS timestamp_str;\n\nOutput: \n\n /*----------------------*\n | timestamp_str |\n +----------------------+\n | 2072-12-25T23:30:00Z |\n *----------------------*/\n\nStatement: \n\n SELECT EXTRACT(HOUR FROM TIMESTAMP(\"2072-12-25 15:30:00\")) AS hour;\n\nOutput: \n\n /*------*\n | hour |\n +------+\n | 23 |\n *------*/\n\nStatement: \n\n SELECT TIMESTAMP_TRUNC(TIMESTAMP \"2072-12-25 15:30:00\", DAY) AS date_str;\n\nOutput: \n\n /*----------------------*\n | date_str |\n +----------------------+\n | 2072-12-25T08:00:00Z |\n *----------------------*/\n\n### Default time zone option set to `Etc/UTC`\n\nThe following examples show how the same statements behave when the\ndefault time zone option is set to `Etc/UTC`.\n\nStatement: \n\n SELECT TIMESTAMP(\"2072-12-25 15:30:00\") AS timestamp_str;\n\nOutput: \n\n /*----------------------*\n | timestamp_str |\n +----------------------+\n | 2072-12-25T15:30:00Z |\n *----------------------*/\n\nStatement: \n\n SELECT EXTRACT(HOUR FROM TIMESTAMP(\"2072-12-25 15:30:00\")) AS hour;\n\nOutput: \n\n /*------*\n | hour |\n +------+\n | 15 |\n *------*/\n\nStatement: \n\n SELECT TIMESTAMP_TRUNC(TIMESTAMP \"2072-12-25 15:30:00+00\", DAY) AS date_str;\n\nOutput: \n\n /*----------------------*\n | date_str |\n +----------------------+\n | 2072-12-25T00:00:00Z |\n *----------------------*/\n\n### Default time zone overridden by function parameter\n\nWhen a function or string literal includes a defined time zone parameter, the\ndatabase's default time zone isn't applied.\n\nStatement: \n\n SELECT FORMAT_TIMESTAMP(\"%c\", TIMESTAMP \"2050-12-25 15:30:55+00\", \"Australia/Sydney\")\n AS formatted;\n\nOutput: \n\n /*--------------------------*\n | formatted |\n +--------------------------+\n | Mon Dec 26 02:30:55 2050 |\n *--------------------------*/\n\nStatement: \n\n SELECT TIMESTAMP(\"2072-12-25 15:30:00+11:00\") AS timestamp_str;\n\nOutput: \n\n /*----------------------*\n | timestamp_str |\n +----------------------+\n | 2072-12-25T04:30:00Z |\n *----------------------*/"]]