Example: Creating a database view in PostgreSQL using the sdetable command

Complexity: Beginner Data Requirement: Use your own data

You can use the sdetable command to create a view in a geodatabase in PostreSQL. Doing so automatically registers the view with ArcSDE.

The example in this topic shows how a view created by sdetable can be used to restrict user access to specific data. The example is based on a table with the following definition:

CREATE TABLE employees(
  emp_id integer not null, 
  name varchar(32), 
  department smallint not null, 
  hire_date timestamp without time zone not null
);

Notice that the table contains a not null integer column that can be used as an ArcSDE row ID.

Create a view using the sdetable command

Suppose you want the manager of department 101 to see all the columns in the table employees but only the rows for employees in department 101. Use sdetable to create a view with all rows in which the department number is 101. The –w option specifies the WHERE clause of the query stored with the view definition.

sdetable –o create_view –T view_dept_101 –t employees 
–c "emp_id,name,department,hire_date" -w "where department = 101" 
–u gdb –p gdb.bdg –i sde:postgresql:mypgdbserver 
-s mypgdbserver -D testdb

Attribute        Administration Utility
__________________________________
Successfully created view view_dept_101.

For more information on the sdetable command, see the Administration Command Reference.

The sdetable command defines the view in the PostgreSQL system catalog, just as if you created the view using SQL. To see this, query the PostgreSQL pg_views system view.

SELECT schemaname,viewname,viewowner,definition
FROM pg_views
WHERE pg_views.viewname = 'view_dept_101';

schemaname

viewname

viewowner

definition

gdb

view_dept_101

gdb

SELECT employees.emp_id,employees.name,employees.department,employees.hire_date FROM gdb.employees WHERE (employees.department = 101);

Notice that the names of the view and its owner are stored in lowercase. Therefore, when querying for these specific text values, you must type them in lowercase.

CautionCaution:

Some users create registered views with sdetable –o create_view and alter the view definition using SQL. This is done to avoid complex sdetable –o create_view command line syntax (especially the –c option) or to include a more complex or extended view definition, such as one that uses connections to external databases. Although altering a view can solve some problems, it is also possible to create a view that does not work well with ArcSDE or ArcSDE clients such as ArcGIS Desktop.

Grant privileges on the view

The view owner can grant privileges on the view to specific users without having to grant those users access to the base table (employees). In this example, the user mgr100 is granted select privileges on the view, view_dept_101:

c:\work>psql testdb gdb
Enter password for gdb:

testdb=> GRANT SELECT ON view_dept_101 TO mgr100;
NoteNote:

In this example, the table owner and the view owner are the same user. If they were different, the table owner would need to grant the view owner privileges to select from the table plus the ability to grant select to other users. If the privilege is granted using SQL, include the WITH GRANT OPTION in the statement. If the privilege is granted using the sdetable command with the grant operation, the inherit (–I) option must be specified.

Test privileges

Log in as mgr100 and select records from view_dept_101.

c:\work> psql testdb mgr100
 Enter password for mgr100:

testdb=> SELECT * FROM gdb.view_dept_101;

emp_id    name        dept    hire_date
29        YAN WU      101     2002-04-15 08:11:27
67        LEE VAN     101     2004-11-01 09:32:01
78        SUE CHOO    101     2005-05-31 08:26:05
105       DAN HO      101     2006-10-01 10:19:52
111       ANN ANG     101     2006-12-15 14:02:03
135       BILL BO     101     2007-10-15 07:57:30

As expected, only records for employees in department 101 are returned.

Views exist as schema objects, independent of the tables that populate them. In the following query, the user mgr100 has no access to the table, gdb.employees; therefore, he cannot query that table.

testdb=> SELECT * FROM gdb.employees;
ERROR: permission denied for relation employees

8/19/2013