How to Install and Manage PostGIS with a Non-Superuser Role
Prerequisite: PostgreSQL Version
This guide assumes you are using PostgreSQL version 14 or later, which supports the necessary commands for PostGIS installation and management. Ensure your PostgreSQL server is up-to-date before proceeding.
This guide ensures that PostGIS is installed and configured properly for a specific user, such as administrator
, while avoiding common issues.
1. Ensure Superuser Access
sudo -i -u postgres
psql --dbname=financethat
2. Create a Role for PostGIS Management
CREATE ROLE administrator WITH LOGIN PASSWORD 'your_secure_password';
GRANT ALL PRIVILEGES ON DATABASE financethat TO administrator;
3. Install PostGIS
To install PostGIS on Ubuntu, first ensure you have the required PostgreSQL version installed. Then, use the following commands:
sudo apt update
sudo apt install postgis postgresql-14-postgis-3
Replace 14
with your PostgreSQL version if different. After installation, enable PostGIS in your database as shown below.
To install PostGIS on macOS, you can use brew
(Homebrew package manager). Follow these steps:
brew update
brew install postgresql postgis
After installation, start the PostgreSQL server and enable PostGIS in your database as shown below.
CREATE EXTENSION postgis;
Verify the installation:
SELECT PostGIS_Full_Version();
4. Transfer Ownership of PostGIS to administrator
ALTER EXTENSION postgis OWNER TO administrator;
ALTER SCHEMA public OWNER TO administrator;
5. Grant Additional Permissions (if needed)
If other users need to access PostGIS functionality:
GRANT USAGE ON SCHEMA public TO other_user;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO other_user;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO other_user;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO other_user;
6. Restore a Database Dump with PostGIS
If restoring from an AWS RDS or similar environment:
- Check for
rdsadmin
references in the dump file: - Remove or replace
rdsadmin
references: - Restore the modified dump file:
gunzip -c your_dump.sql.gz | grep -i rdsadmin
REVOKE ALL ON SCHEMA public FROM rdsadmin;
GRANT ALL ON SCHEMA public TO administrator;
gunzip -c your_dump.sql.gz | psql --username=administrator --dbname=financethat
7. Verify PostGIS Installation
Log in as administrator
:
psql --username=administrator --dbname=financethat
Test PostGIS functionality:
SELECT PostGIS_Full_Version();
SELECT ST_AsText(ST_MakePoint(0, 0));
Check for installed extensions:
\\dx
8. Notes for Future Use
- Always ensure the target user (
administrator
) has sufficient privileges for the database and schema. - When restoring from AWS RDS, clean up
rdsadmin
references in the dump file. - If recreating PostGIS, drop the extension and recreate it under the target user's ownership:
DROP EXTENSION postgis CASCADE;
CREATE EXTENSION postgis;
This process ensures a smooth PostGIS installation and management under a non-superuser role like administrator
. Let me know if further clarifications are needed!
Comments
Post a Comment