This section describes how to set up
pg_featureserv and connect the service to a spatial database.
The first half walks through how to prepare a spatial database and import spatial data, using the terminal. If you already have a spatial database, you can go ahead and start with “Configuring the service.”
The following terminal command creates a new database named
(assuming your user role has the create database privilege):
psql tool, load the PostGIS extension as superuser (we’ll go with
psql -U postgres -d naturalearth -c 'CREATE EXTENSION postgis'
We’re going to be tidy and load the data into a schema
To create the schema, run the command:
psql -U postgres -d naturalearth -c 'CREATE SCHEMA ne'
When we get to the step below to connect
pg_featureserv to the database, the user must have access to the new schema as well.
The data used in the examples are loaded from Natural Earth. Download the Admin 0 - Countries ZIP and extract to a directory on your computer.
In that directory, run the following terminal command to load the
data into the
ne schema in the
This creates a new table
countries, with the application user as the owner.
shp2pgsql -D -s 4326 ne_50m_admin_0_countries.shp ne.countries | psql -U <username> -d naturalearth
You should see the
ne.countries table using the
\dt ne.* command in the
psql SQL shell.
Configuring the service
Make sure that the service database connection specifies the
As described in the Configuration section,
this can be provided either by an environment variable:
Or by a configuration file parameter:
DbConnection = "postgresql://username:password@localhost/naturalearth"
Download the build of the latest code:
Unzip the file, copy the
pg_featureserv binary wherever you wish, or use it in place. (If you move the binary, remember to move the
assets/ directory to the same location, or start the server using the
AssetsDir configuration option.)
In the directory where the
pg_featureserv binary is located, run the service in the terminal:
With the service running, you should see the layer listed on the web user interface at http://localhost:9000/collections.html. The layer metadata is viewable at http://localhost:9000/collections/ne.countries.html.