Skip to content

Instantly share code, notes, and snippets.

@johnjreiser
Last active April 29, 2024 14:55
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save johnjreiser/24c8267d8fa0a866fdf352f1911a1c40 to your computer and use it in GitHub Desktop.
Save johnjreiser/24c8267d8fa0a866fdf352f1911a1c40 to your computer and use it in GitHub Desktop.
Script to install PostgreSQL 13 and PostGIS 3.2 on fresh Amazon Linux 2
#!/bin/bash
# Script to install PostgreSQL and PostGIS on a fresh Amazon Linux instance
# Installing from source:
# - GEOS
# GEOS 3.10+ requires CMake 3+, not readily available on Amazon Linux 2.
GEOSVER=3.9.2
GEOSURL=http://download.osgeo.org/geos/geos-${GEOSVER}.tar.bz2
# - PROJ (GDAL requires 6+; 6.2.1 is the last to use SQLite 3.7; 6.2 had build issues, so 6.1.1)
PROJVER=6.1.1
PROJURL=https://download.osgeo.org/proj/proj-${PROJVER}.tar.gz
# - GDAL
GDALVER=3.4.3
GDALURL=https://github.com/OSGeo/gdal/releases/download/v${GDALVER}/gdal-${GDALVER}.tar.gz
# - PostGIS
POSTGISVER=3.2.1
POSTGISURL=https://download.osgeo.org/postgis/source/postgis-${POSTGISVER}.tar.gz
set -e
sudo amazon-linux-extras install postgresql13 vim epel -y
sudo yum-config-manager --enable epel -y
sudo yum update -y
sudo yum install -y make automake cmake gcc gcc-c++ libcurl-devel proj-devel pcre-devel autoconf automake libxml2-devel libjpeg-turbo-static libjpeg-turbo-devel sqlite-devel
sudo yum install -y clang llvm
sudo yum install -y postgresql-server postgresql-server-devel
############################
# Install GEOS from Source #
############################
curl -O $GEOSURL
tar xvjf geos-${GEOSVER}.tar.bz2
rm -f geos-${GEOSVER}.tar.bz2
cd geos-${GEOSVER}
./configure
make
sudo make install
cd
############################
# Install PROJ from Source #
############################
curl -L -O $PROJURL
tar xvzf proj-${PROJVER}.tar.gz
rm -f proj-${PROJVER}.tar.gz
cd proj-${PROJVER}
./configure
make
sudo make install
cd
############################
# Install GDAL from Source #
############################
curl -L -O $GDALURL
tar xvzf gdal-${GDALVER}.tar.gz
rm -f gdal-${GDALVER}.tar.gz
cd gdal-${GDALVER}
./configure \
--prefix=${PREFIX} \
--with-geos \
--with-proj=/usr/local \
--with-geotiff=internal \
--with-hide-internal-symbols \
--with-libtiff=internal \
--with-libz=internal \
--with-threads \
--without-bsb \
--without-cfitsio \
--without-cryptopp \
--with-curl \
--without-dwgdirect \
--without-ecw \
--without-expat \
--without-fme \
--without-freexl \
--without-gif \
--without-gif \
--without-gnm \
--without-grass \
--without-grib \
--without-hdf4 \
--without-hdf5 \
--without-idb \
--without-ingres \
--without-jasper \
--without-jp2mrsid \
--with-jpeg=internal \
--without-kakadu \
--without-libgrass \
--without-libkml \
--without-libtool \
--without-mrf \
--without-mrsid \
--without-mysql \
--without-netcdf \
--without-odbc \
--without-ogdi \
--without-openjpeg \
--without-pcidsk \
--without-pcraster \
--with-pcre \
--without-perl \
--with-pg \
--without-php \
--with-png=internal \
--without-python \
--without-qhull \
--without-sde \
--without-sqlite3 \
--without-webp \
--with-xerces \
--with-xml2
make
sudo make install
cd
###################################
# Install PostGIS from source #
###################################
curl -O $POSTGISURL
tar xvzf postgis-${POSTGISVER}.tar.gz
rm -f postgis-${POSTGISVER}.tar.gz
cd postgis-${POSTGISVER}
./configure --with-address-standardizer --without-protobuf
make
sudo make install
cd
###################
# Final Prep Work #
###################
sudo ln -s /usr/local/lib/libgeos_c.so.1 /usr/lib64/pgsql/libgeos_c.so.1
sudo sh -c 'echo /usr/local/lib > /etc/ld.so.conf.d/postgresql.conf'
sudo sh -c 'echo /usr/lib64/pgsql >> /etc/ld.so.conf.d/postgresql.conf'
sudo ldconfig -v
export PGHOME=/var/lib/pgsql/data/
sudo su postgres -c "pg_ctl -D $PGHOME initdb"
sudo systemctl enable postgresql
sudo systemctl start postgresql
echo "
Your system is now running PostgreSQL with PostGIS.
You should now run "aws configure" to set up the AWS CLI.
Afterwards, you should stop this instance and create an AMI.
"
@achillis2
Copy link

This script installs postgresql10 which is the old version. Can you use it to install postgresql12? Thanks

@kandoigaurav
Copy link

Trying this for postgresql12 on Amazon Linux 2 and running into the following:

(base) [ec2-user@ip-XX-XX-XXX-XXX ~]$ sudo yum install -y postgresql12-server postgresql12-devel
Loaded plugins: extras_suggestions, langpacks, update-motd
Package postgresql12-server-12.11-1PGDG.rhel7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package postgresql12-devel.x86_64 0:12.11-1PGDG.rhel7 will be installed
--> Processing Dependency: llvm5.0-devel >= 5.0 for package: postgresql12-devel-12.11-1PGDG.rhel7.x86_64
--> Processing Dependency: llvm-toolset-7-clang >= 4.0.1 for package: postgresql12-devel-12.11-1PGDG.rhel7.x86_64
--> Running transaction check
---> Package llvm5.0-devel.x86_64 0:5.0.1-7.el7 will be installed
---> Package postgresql12-devel.x86_64 0:12.11-1PGDG.rhel7 will be installed
--> Processing Dependency: llvm-toolset-7-clang >= 4.0.1 for package: postgresql12-devel-12.11-1PGDG.rhel7.x86_64
--> Finished Dependency Resolution
Error: Package: postgresql12-devel-12.11-1PGDG.rhel7.x86_64 (pgdg12)
           Requires: llvm-toolset-7-clang >= 4.0.1
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

@johnjreiser
Copy link
Author

Updated this script to use PostgreSQL 13 and PostGIS 3.2.

@johnjreiser
Copy link
Author

Pushed another update this morning to address a typo and missing requirement. You should be able to run this script on a fresh Amazon Linux install and end up with PostgreSQL 13 with PostGIS 3.2.

Please comment if you have any issues. If you find & resolve an issue, please open a PR!

Hope this helps!

@johnjreiser
Copy link
Author

Also, if you require common extensions (e.g. uuid, fuzzystrmatch) make sure you sudo yum install postgresql-contrib.

@FrankDupree
Copy link

incase anyone stumbles across this and you tried installing the latest individually.
i.e postgresql14 with paths like /usr/pgsql-14/
this wont work for you:

sudo ln -s /usr/local/lib/libgeos_c.so.1 /usr/lib64/pgsql/libgeos_c.so.1
sudo sh -c 'echo /usr/local/lib > /etc/ld.so.conf.d/postgresql.conf'
sudo sh -c 'echo /usr/lib64/pgsql >> /etc/ld.so.conf.d/postgresql.conf'
sudo ldconfig -v

use this instead:

sudo su
echo /usr/local/lib >> /etc/ld.so.conf
exit
sudo ldconfig

@dwburns
Copy link

dwburns commented Oct 12, 2022

I got a conflict between postgresql-devel and libpq-devel when running a fresh install. Changing line 29 to the following fixed it.

sudo yum install -y postgresql-server postgresql-server-devel

@johnjreiser
Copy link
Author

I got a conflict between postgresql-devel and libpq-devel when running a fresh install. Changing line 29 to the following fixed it.

Thank you, this has been incorporated.

@johnjreiser
Copy link
Author

Note to others - while this includes the PostGIS extension, it does not include other common (e.g. fuzzystrmatch) extensions. To add them, simply run: sudo yum install -y postgresql-contrib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment