Skip to content

Commit 12bcdd5

Browse files
Fix: correct migration directory name and expand bootstrapping instructions for PgSTAC (#367)
1 parent f96cbde commit 12bcdd5

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

docs/src/pypgstac.md

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ There are two types of migrations:
5757
- **Base migrations** install PgSTAC into a database with no current PgSTAC installation. These migrations follow the file pattern `"pgstac.[version].sql"`
5858
- **Incremental migrations** are used to move PgSTAC from one version to the next. These migrations follow the file pattern `"pgstac.[version].[fromversion].sql"`
5959

60-
Migrations are stored in ```pypgstac/pypgstac/migration`s``` and are distributed with the pyPgSTAC package.
60+
Migrations are stored in ```pypgstac/pypgstac/migrations``` and are distributed with the pyPgSTAC package.
6161

6262
### Running Migrations
6363
pyPgSTAC has a utility for checking the version of an existing PgSTAC database and applying the appropriate migrations in the correct order. It can also be used to setup a database from scratch.
@@ -67,6 +67,97 @@ To create an initial PgSTAC database or bring an existing one up to date, check
6767
pypgstac migrate
6868
```
6969

70+
### Bootstrapping an Empty Database
71+
72+
When starting with an empty database, you have two options for initializing PgSTAC:
73+
74+
#### Option 1: Execute as Power User
75+
76+
This approach uses a database user with administrative privileges (such as 'postgres') to run the migration, which will automatically create all necessary extensions and roles:
77+
78+
```bash
79+
# Set environment variables for database connection
80+
export PGHOST=localhost
81+
export PGPORT=5432
82+
export PGDATABASE=yourdatabase
83+
export PGUSER=postgres # A user with admin privileges
84+
export PGPASSWORD=yourpassword
85+
86+
# Run the migration
87+
pypgstac migrate
88+
```
89+
90+
The migration process will automatically:
91+
- Create required extensions (postgis, btree_gist, unaccent)
92+
- Create necessary roles (pgstac_admin, pgstac_read, pgstac_ingest)
93+
- Set up the pgstac schema and tables
94+
95+
In production environments, you should assign these roles to your application database user rather than continuing to use the postgres user:
96+
97+
```sql
98+
-- Grant appropriate roles to your application user
99+
GRANT pgstac_read TO your_app_user;
100+
GRANT pgstac_ingest TO your_app_user;
101+
GRANT pgstac_admin TO your_app_user;
102+
103+
-- Set the search path for your application user
104+
ALTER USER your_app_user SET search_path TO pgstac, public;
105+
```
106+
107+
#### Option 2: Create User with Initial Grants
108+
109+
If you don't have administrative privileges or prefer more control over the setup process, you can manually prepare the database before running migrations.
110+
111+
Connect to your database as an administrator and execute:
112+
113+
```sql
114+
\c [database]
115+
116+
-- Create required extensions
117+
CREATE EXTENSION IF NOT EXISTS postgis;
118+
CREATE EXTENSION IF NOT EXISTS btree_gist;
119+
CREATE EXTENSION IF NOT EXISTS unaccent;
120+
121+
-- Create required roles
122+
CREATE ROLE pgstac_admin;
123+
CREATE ROLE pgstac_read;
124+
CREATE ROLE pgstac_ingest;
125+
126+
-- Grant appropriate permissions
127+
ALTER DATABASE [database] OWNER TO [user];
128+
ALTER USER [user] SET search_path TO pgstac, public;
129+
ALTER DATABASE [database] set search_path to pgstac, public;
130+
GRANT CONNECT ON DATABASE [database] TO [user];
131+
GRANT ALL PRIVILEGES ON TABLES TO [user];
132+
GRANT ALL PRIVILEGES ON SEQUENCES TO [user];
133+
GRANT pgstac_read TO [user] WITH ADMIN OPTION;
134+
GRANT pgstac_ingest TO [user] WITH ADMIN OPTION;
135+
GRANT pgstac_admin TO [user] WITH ADMIN OPTION;
136+
```
137+
138+
Then run the migration as your non-admin user:
139+
140+
```bash
141+
# Set environment variables for database connection
142+
export PGHOST=localhost
143+
export PGPORT=5432
144+
export PGDATABASE=yourdatabase
145+
export PGUSER=[user] # Your non-admin user
146+
export PGPASSWORD=yourpassword
147+
148+
# Run the migration
149+
pypgstac migrate
150+
```
151+
152+
### Verifying Migration
153+
154+
To verify that PgSTAC was installed correctly:
155+
156+
```bash
157+
# Check the PgSTAC version
158+
pypgstac version
159+
```
160+
70161
### Bulk Data Loading
71162
A python utility is included which allows to load data from any source openable by smart-open using python in a memory efficient streaming manner using PostgreSQL copy. There are options for collections and items and can be used either as a command line or a library.
72163

0 commit comments

Comments
 (0)