brite, a MVC Framework for jQuery

Amazon EC2: Installing Postgres 9.3 on Amazon Linux & Instance Local Storage

November 20th, 2013 by jeremychone

Update 2016-03-26

Postgres is now providing yum installs for the main releases!!! See Yum Install Postgres 9.5 on amazon linux

 

Below kept for reference

Unfortunately, the latest postgres 9.3 is not available from the default Amazon Linux package manager (yum …). There are two options to install it:

Option 1) Install from rpm/yum from postgres repo

In the files /etc/yum.repos.d/amzn-main.repo
and /etc/yum.repos.d/amzn-updates.repo add the following in the
block [amzn-main]:

exclude=postgresql*

Then, install the repo rpm and run yum

# Change to home directory to download the software
cd ~/
# Get the right postgresql package (Redhat 64 Bit)
wget http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-redhat93-9.3-1.noarch.rpm
# Install the package
sudo rpm -ivh pgdg-redhat93-9.3-1.noarch.rpm
sudo yum install postgresql93 postgresql93-server postgresql93-devel

# Check that the service is installed
sudo service postgresql-9.3 status
# should return "is stopped"

# init the DB
sudo service postgresql-9.3 initdb
# Start the DB
sudo service postgresql-9.3 start

# connect
sudo su - postgres
psql

In the psql, just do a l

postgres=# l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres

source

Option 2) From source

Here is how to install Postgres 9.3 from source on Amazon Linux.

In addition, optional steps to set the postgres data on a ephemeral storage with a little convenient script to reinitialize database after shutdown (great for dev, testing, and even production if robust failover/backup setup). The ephemeral disk is assumed to be mounted as /local as shown in the previous Install Amazon AMI with instance storage post.

# Become root
sudo su -

# Install C compiler (not installed by default on Amazon Linux)
yum -y install gcc

# Install the devel of openssl zlib and readline
yum  -y  install openssl-devel
yum  -y  install zlib-devel
yum  -y  install readline-devel

# Create the user
useradd postgres

# Here are the folders we will be using for runtime, log, and data
mkdir  -p /usr/local/pgsql
mkdir  -p /var/log/pgsql
mkdir  -p /var/data/pgsql

# For Variant: Note that for the ephemeral disk mounted on "/local"
#                  we can make the log and data on the local
# mkdir  -p /var/data/
# mkdir  -p /var/log/
# mkdir -p /local/var/data/pgsql
# mkdir -p /local/var/log/pgsql
# ln -s /local/var/data/pgsql /var/data/pgsql
# ln -s /local/var/log/pgsql /var/log/pgsql

# Download and make latest postgres (check for the latest)
cd /usr/local/src
wget  "http://ftp.postgresql.org/pub/source/v9.3.1/postgresql-9.3.1.tar.gz"
tar -zxvf postgresql-9*
cd postgresql-9*

./configure  --prefix=/usr/local/pgsql --with-openssl
make
make install

# Adjustment of Permission.
chown  -R  postgres:postgres  /usr/local/pgsql
chown  -R  postgres:postgres  /var/log/pgsql
chown  -R  postgres:postgres  /var/data/pgsql

# For Variant: Also need to change the perms of the real directories
# chown  -R  postgres:postgres /local/var/data/pgsql
# chown  -R  postgres:postgres /local/var/log/pgsql

# init the db
su postgres -c'/usr/local/pgsql/bin/initdb --encoding=UTF-8 --no-locale -D /var/data/pgsql'

# setup
cp /usr/local/src/postgresql-9.3.1/contrib/start-scripts/linux /etc/init.d/postgres
chmod 700 /etc/init.d/postgres

#### DO: Edit the /etc/init.d/postgres to update both variables as follow
# PGDATA=”/var/data/pgsql”
# PGLOG=”/var/log/pgsql/serverlog”
#####

# Note: for the Instance variant, probably useless, but good for EBS ones.
chkconfig postgres on

# Start the postgres
/etc/init.d/postgres start
# or "service postgres start"

# Add the pgsql/bin to the postgres user path
sudo su - postgres
vi .bash_profile
# DO: add the pgsql/bin to the PATH (like: "PATH=$PATH:$HOME/bin:/usr/local/pgsql/bin/")

# Should be in business
psql

Source

Installing pg_trm

$ cd /usr/local/src/postgresql-9.3.1/contrib/pg_trgm
$ make
$ sudo make install # or su -c 'make install' if you don't use sudo
Then, in the database of chose from psql
create extension pg_trgm;

To have it activated by default on any database created in the future, apply this command to the template1 database.

Source

Only for instance storage Variant

Now, assuming the instance store variant was used, you can add a initdb.sh on ~ec2-user/ that you will call after the instance start (can probably even make it run on instance start)

mkdir -p /local/var/data/pgsql
mkdir -p /local/var/log/pgsql

chown  -R  postgres:postgres /local/var/data/pgsql
chown  -R  postgres:postgres /local/var/log/pgsql

su postgres -c'/usr/local/pgsql/bin/initdb --encoding=UTF-8 --no-locale -D /var/data/pgsql'

Comments are closed.