From 81f4f93a8dc4098b606b0dd9ea1d42ea4e6dbca7 Mon Sep 17 00:00:00 2001 From: fiatfilip Date: Mon, 3 Apr 2017 16:39:08 +0300 Subject: [PATCH] Create 2017-04-03-Set-up-PostgreSQL-on-Azure.md Creating a VM in Azure and install PostgreSQL into it --- .../2017-04-03-Set-up-PostgreSQL-on-Azure.md | 180 ++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 _posts/2017-04-03-Set-up-PostgreSQL-on-Azure.md diff --git a/_posts/2017-04-03-Set-up-PostgreSQL-on-Azure.md b/_posts/2017-04-03-Set-up-PostgreSQL-on-Azure.md new file mode 100644 index 0000000000000..c3b48b2cfc734 --- /dev/null +++ b/_posts/2017-04-03-Set-up-PostgreSQL-on-Azure.md @@ -0,0 +1,180 @@ +--- +layout: post +title: Set up PostgreSQL on Azure +subtitle: One script away from a PostgreSQL running into an Ubuntu powered VM running on Azure +category: Howto +tags: [cloud] +author: Filip Fiat +author_email: filip.fiat@haufe-lexware.com +header-img: "images/bg-post.jpg" +--- + +These days on can find tons of tutorials and recipes on how to install a PostgreSQL server on Azure. + +Basically there are (at least) two ways of doing this. First, and the easiest, just go to the web dashboard and spin up a preconfigured VM. Another option would be to create just a Linux machine and install PostgreSQL directly. This can also be done with a couple of clicks in the web bashboard, but any respectable developer ;) would want it scripted, fully reproducible and cutomizable! + +That's what I wanted to do and found here https://docs.microsoft.com/en-us/azure/virtual-machines/virtual-machines-linux-postgresql-install just the right blog post to copy-and-paste my install script from. +Well... almost, because copy-and-paste never works and there is almost always somethink to tweak. + +This blog post basically follows the steps from the previous link, but adapts and corrects them in order to be comprised within one script (actually two scripts ;), but one of them is only used to create the virtual machine in Azure, copy and launch the other into that machine. This can also be done with three commands see below, so this hardly can count for a script. + +Following we'll cover these steps: +- create VM +- install PostgreSQL +- configure PostgreSQL +- setup PostgreSQL +- start PostgreSQL server. + +>Prerequisite: have latest docker-machine installed. + +### 1. Creating the VM in AzureDE + +This is pretty straightforward, just export these properties +```sh +export DOCKER_LOCAL=".docker/machine/" +export AZURE_SUBSCRIPTION="********-****-****-****-************" +export AZURE_IMAGE="canonical:UbuntuServer:16.04.0-LTS:latest" +export AZURE_CLIENT_SECRET="*********************************=" +export AZURE_CLIENT_ID="********-****-****-****-************" +export AZURE_ENVIRONMENT="AzureGermanCloud" +export AZURE_LOCATION="germanycentral" +export AZURE_SIZE="Standard_DS3_V2" +export AZURE_RESOURCE_GROUP="##########################" +export AZURE_SUBNET="######################" +export AZURE_SUBNET_PREFIX="###.###.###.###/##" +export AZURE_AVAILABILITY_SET="*******************-AS" +export AZURE_VNET="************************-vNet" +``` +and create the machine: + +```sh +docker-machine -s ${DOCKER_LOCAL} create -d azure --azure-environment=${AZURE_ENVIRONMENT} \ + --azure-location=${AZURE_LOCATION} \ + --azure-no-public-ip \ + --azure-size=${AZURE_SIZE} \ + --azure-resource-group=${AZURE_RESOURCE_GROUP} \ + --azure-subnet=${AZURE_SUBNET} \ + --azure-subnet-prefix=${AZURE_SUBNET_PREFIX} \ + --azure-image=${AZURE_IMAGE} \ + --azure-availability-set=${AZURE_AVAILABILITY_SET} \ + --azure-vnet=${AZURE_VNET} \ + --azure-ssh-user=ubuntu \ + --azure-subscription-id=${AZURE_SUBSCRIPTION} \ + --azure-client-secret ${AZURE_CLIENT_SECRET} \ + --azure-client-id ${AZURE_CLIENT_ID} ${AZURE_MACHINE_NAME} +``` +Afterwards, just copy there the install script and run it! +```sh +docker-machine -s ${DOCKER_LOCAL} scp install_postgresql.sh ${AZURE_MACHINE_NAME}:install_postgresql.sh +docker-machine -s ${DOCKER_LOCAL} ssh ${AZURE_MACHINE_NAME} ./install_postgresql.sh +``` +Installing, configuring and starting the PostgreSQL server is done with the `install_postgresql.sh` script and all the commands from now one will be placed into this script. +>All commands must be run with admin rights, so... `sudo`. + +### 2. Install PostgreSQL + +- Some distributions have dependencies that you must install before installing PostgreSQL, +```sh +sudo apt-get install gcc make openssl libreadline-dev zlib1g-dev -y +``` +On Microsoft's website (see above) there are some more packages listed but they are not necessary (at least I did not used them). Furthermore `readline-devel` is actually replaced with `libreadline-dev` and `zlib-devel` is `zlib1g-dev`. + +- Download PostgreSQL into the root directory, and then unzip the package +```sh +sudo wget https://ftp.postgresql.org/pub/source/v9.3.11/postgresql-9.3.11.tar.bz2 -P /root/ +sudo tar jxvf /root/postgresql-9.3.11.tar.bz2 -C /root/ +``` +- Start building +```sh +sudo /root/postgresql-9.3.11/configure --prefix=/opt/postgresql-9.3.11 +sudo make install /root/postgresql-9.3.11 +``` + +### 3. Configure PostgreSQL + +- Create a symbolic link to shorten the PostgreSQL reference to not include the version number (this is optional, but useful) and the directory for the database +```sh +sudo ln -s /opt/postgresql-9.3.11 /opt/pgsql +sudo mkdir -p /opt/pgsql_data +``` +- Create a non-root user and modify that user’s profile. For security reasons, PostgreSQL uses a non-root user to initialize, start, or shut down the database. +```sh +sudo useradd -d /home/postgres -m postgres +sudo cat >> ~/.dbuserpw <> ~/.dbuserpw <> ~/.bash_profile <> ~/.dbuserpw <