Example: Creating a spatial view in PostgreSQL using SQL

Complexity: Beginner Data Requirement: Use your own data

You can use SQL to create a spatial view by including the spatial column in the view definition. You would do this so you can view the features in ArcMap. You might also create a spatial view to allow you to use a table that contains more than one spatial column; your spatial view would only include one spatial column so you can use it with ArcGIS.

In this example, a spatial view is created to join data from the employees table and the region feature class.

Create a view with a spatial column

Define the view to include the spatial column and ObjectID from the feature class along with the other attribute columns you want in the view.

The owner of the employees table and region feature class is the gdb user; therefore, the user already has the necessary privileges to create the view.

TipTip:

To create a view, the user must have at least select privileges on each table or feature class included in the view.

psql testdb gdb
Enter password for user gdb:

CREATE VIEW emp_region_view
AS SELECT (
	employees.emp_name,employees.emp_id,
	hbear.region.rname,
	hbear.region.reg_id,
	hbear.region.region)
	FROM employees, hbear.regions
	WHERE employees.emp_id = hbear.regions.emp_id;

The reg_id is the not null, integer ObjectID column from the region feature class. Region is the spatial column from the region feature class. The tables are joined based on the emp_id column.

Grant privileges on the spatial view

Now that the view exists, grant select privileges to dispatch_mgr . Include WITH GRANT OPTION to allow dispatch_mgr to grant privileges on the view to other users, groups, or roles.

GRANT SELECT 
ON gdb.emp_region_view
TO dispatch_mgr WITH GRANT OPTION;
11/6/2014