By Sanni Kehinde, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud's incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.
In this guide, we will install and set up a PostgreSQL database on an Ubuntu instance using Alibaba Cloud Elastic Compute Service (ECS).
But before we begin, it is important to know that there are different ways to set up a PostgreSQL database on any cloud provider. For Alibaba Cloud, you can create an instance and set it up manually using Elastic Compute Service (ECS) or by using ApsaraDB RDS.
So before I proceed with the guide, I would point out some few differences between using an ECS or ApsaraDB RDS for your database.
For ECS
For ApsaraDB RDS (Relational Database Service)
To follow along with this guide, you need to have an instance installed with Ubuntu OS. You can also check this guide on How to create an instance.
After completing the prerequisites, follow the steps below
SSH
into your instance using your key pair for Linux user. For windows, you can use SSH client such as putty to connect to your instance.sudo apt-get update && sudo apt-get upgrade
sudo apt-get install postgresql
psql -V
To edit the configuration file(pg_hba.conf
), run sudo vim /etc/postgresql/9.5/main/pg_hba.conf
.
9.5
is the version of postgreSQL installed. As at the time of writing this article,
9.5 is the default installation for postgreSQL on ubuntu OS.
pg_hba.conf
in vim, you can use any editor of your choice. Update the file which read something like thisby default
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
to
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 md5
This would enable us connect to our database instance.
postgresql.conf
file to enable remote connection from anywhere(IP address).To edit postgresql.conf
file run sudo vim /etc/postgresql/9.5/main/postgresql.conf
. Use :set number
to enable the line numbers and look for line 59 which should be like this
#listen_addresses = 'localhost'
Update it to this
listen_addresses = '*'
sudo /etc/init.d/postgresql restart
sudo - su postgres
psql
The psql
command gives us access to the Postgres interactive terminal
CREATE USER ubuntu_user SUPERUSER;
ALTER USER power_user WITH PASSWORD 'password';
CREATE DATABASE mydatabase WITH OWNER power_user;
\q
. We can view the content of our database and make changes to it with psql. We can also connect to our database using a database client.We would be using a database client called postico. you can use any database client of your choices.
new favourite
to add your connection parametersThe field parameters in the screenshot above are explained below
nickname
field can be anythingHost
field contains the public IP address of our ECS instanceUser
field contains the username we created earlier which is ubuntu_user
password
field is for the password we created for the ubuntu_user
which is password
mydatabase
.With ApsaraDB RDS you can get a database instance up and running in minutes. But if you are looking for full control such as configuration and security, then Elastic Compute Service (ECS) is definitely the way to go.
Deploying Static Websites Using OSS and CDN on Alibaba Cloud
2,599 posts | 764 followers
FollowAlibaba Clouder - June 5, 2019
Alibaba Clouder - June 10, 2019
Alibaba Clouder - June 4, 2019
Alibaba Clouder - June 10, 2019
Alibaba Clouder - December 26, 2018
Alibaba Clouder - March 22, 2019
I was impressed with the depth of analysis and attention to detail in your article.
2,599 posts | 764 followers
FollowElastic and secure virtual cloud servers to cater all your cloud hosting needs.
Learn MoreLearn More
An encrypted and secure cloud storage service which stores, processes and accesses massive amounts of data from anywhere in the world
Learn MoreMore Posts by Alibaba Clouder
Raja_KT February 16, 2019 at 6:03 am
Good one.... True...if you are looking for full control such as configuration and security, then Elastic Compute Service (ECS) is definitely the way to go.