You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: v20.2/alter-schema.md
+55-49
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ Parameter | Description
31
31
32
32
### Rename a schema
33
33
34
-
Suppose that you access the [SQL shell](cockroach-sql.html) as user `demo`, and [create a new user](create-user.html)`max` and [a schema](create-schema.html)`org_one` with `max` as the owner:
34
+
Suppose that you access the [SQL shell](cockroach-sql.html) as user `root`, and [create a new user](create-user.html)`max` and [a schema](create-schema.html)`org_one` with `max` as the owner:
35
35
36
36
{% include copy-clipboard.html %}
37
37
~~~sql
@@ -49,14 +49,14 @@ Suppose that you access the [SQL shell](cockroach-sql.html) as user `demo`, and
49
49
~~~
50
50
51
51
~~~
52
-
schema_name | owner
53
-
---------------------+--------
54
-
crdb_internal | NULL
55
-
information_schema | NULL
56
-
org_one | max
57
-
pg_catalog | NULL
58
-
pg_extension | NULL
59
-
public | admin
52
+
schema_name
53
+
----------------------
54
+
crdb_internal
55
+
information_schema
56
+
org_one
57
+
pg_catalog
58
+
pg_extension
59
+
public
60
60
(6 rows)
61
61
~~~
62
62
@@ -72,9 +72,9 @@ ERROR: must be owner of schema "org_one"
72
72
SQLSTATE: 42501
73
73
~~~
74
74
75
-
Because you are executing the `ALTER SCHEMA` command as a non-owner of the schema (i.e., `demo`), CockroachDB returns an error.
75
+
Because you are executing the `ALTER SCHEMA` command as a non-owner of the schema (i.e., `root`), CockroachDB returns an error.
76
76
77
-
[Drop the schema](drop-schema.html) and create it again, this time with with `demo` as the owner.
77
+
[Drop the schema](drop-schema.html) and create it again, this time with with `root` as the owner.
78
78
79
79
{% include copy-clipboard.html %}
80
80
~~~sql
@@ -86,21 +86,24 @@ Because you are executing the `ALTER SCHEMA` command as a non-owner of the schem
86
86
> CREATE SCHEMA org_one;
87
87
~~~
88
88
89
+
To verify that the owner is now `root`, query the [`pg_catalog.pg_namespace` and `pg_catalog.pg_users` tables](sql-name-resolution.html#databases-with-special-names):
90
+
89
91
{% include copy-clipboard.html %}
90
92
~~~sql
91
-
> SHOW SCHEMAS;
93
+
>SELECT
94
+
nspname, usename
95
+
FROM
96
+
pg_catalog.pg_namespace
97
+
LEFT JOINpg_catalog.pg_userONpg_namespace.nspowner=pg_user.usesysid
98
+
WHERE
99
+
nspname LIKE'org_one';
92
100
~~~
93
101
94
102
~~~
95
-
schema_name | owner
96
-
---------------------+--------
97
-
crdb_internal | NULL
98
-
information_schema | NULL
99
-
org_one | demo
100
-
pg_catalog | NULL
101
-
pg_extension | NULL
102
-
public | admin
103
-
(6 rows)
103
+
nspname | usename
104
+
----------+----------
105
+
org_one | root
106
+
(1 row)
104
107
~~~
105
108
106
109
As its owner, you can rename the schema:
@@ -116,20 +119,20 @@ As its owner, you can rename the schema:
116
119
~~~
117
120
118
121
~~~
119
-
schema_name | owner
120
-
---------------------+--------
121
-
crdb_internal | NULL
122
-
information_schema | NULL
123
-
org_two | demo
124
-
pg_catalog | NULL
125
-
pg_extension | NULL
126
-
public | admin
122
+
schema_name
123
+
----------------------
124
+
crdb_internal
125
+
information_schema
126
+
org_two
127
+
pg_catalog
128
+
pg_extension
129
+
public
127
130
(6 rows)
128
131
~~~
129
132
130
133
### Change a schema's owner
131
134
132
-
Suppose that you access the [SQL shell](cockroach-sql.html) as user `demo`, and [create a new schema](create-schema.html) named `org_one`:
135
+
Suppose that you access the [SQL shell](cockroach-sql.html) as user `root`, and [create a new schema](create-schema.html) named `org_one`:
133
136
134
137
{% include copy-clipboard.html %}
135
138
~~~sql
@@ -142,22 +145,22 @@ Suppose that you access the [SQL shell](cockroach-sql.html) as user `demo`, and
142
145
~~~
143
146
144
147
~~~
145
-
schema_name | owner
146
-
---------------------+--------
147
-
crdb_internal | NULL
148
-
information_schema | NULL
149
-
org_one | demo
150
-
pg_catalog | NULL
151
-
pg_extension | NULL
152
-
public | admin
148
+
schema_name
149
+
----------------------
150
+
crdb_internal
151
+
information_schema
152
+
org_one
153
+
pg_catalog
154
+
pg_extension
155
+
public
153
156
(6 rows)
154
157
~~~
155
158
156
159
Now, suppose that you want to change the owner of the schema `org_one` to an existing user named `max`. To change the owner of a schema, the current owner must belong to the role of the new owner (in this case, `max`), and the new owner must have `CREATE` privileges on the database.
157
160
158
161
{% include copy-clipboard.html %}
159
162
~~~sql
160
-
>GRANT max TO demo;
163
+
>GRANT max TO root;
161
164
~~~
162
165
163
166
{% include copy-clipboard.html %}
@@ -170,21 +173,24 @@ Now, suppose that you want to change the owner of the schema `org_one` to an exi
170
173
> ALTER SCHEMA org_one OWNER TO max;
171
174
~~~
172
175
176
+
To verify that the owner is now `max`, query the [`pg_catalog.pg_namespace` and `pg_catalog.pg_users` tables](sql-name-resolution.html#databases-with-special-names):
177
+
173
178
{% include copy-clipboard.html %}
174
179
~~~sql
175
-
> SHOW SCHEMAS;
180
+
>SELECT
181
+
nspname, usename
182
+
FROM
183
+
pg_catalog.pg_namespace
184
+
LEFT JOINpg_catalog.pg_userONpg_namespace.nspowner=pg_user.usesysid
0 commit comments