Setting Up A PostgreSQL Database on An Ubuntu Instance
Setting Up A PostgreSQL Database on An Ubuntu Instance
So much Ubuntu, so little time.
Join the DZone community and get the full member experience.Join For Free
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.
- You have an instance which you can SSH into
- You are free to install any software of your choice but you are responsible for the license validity.
- You have full control over the configuration of the database, including any performance tweaking you want to do.
- You own the responsibility of the DB's uptime and health as it becomes an app running in the ECS.
- You have to do full system administration yourself, including OS maintenance, security, patches, etc.
For ApsaraDB RDS (Relational Database Service)
- You don't need to worry about the Database health or uptime, as everything is already been taken care of by Alibaba Cloud
- You can't SSH into ApsaraDB RDS DB instance but you can connect to the RDS instance via any internet enabled system or application provided that the security group has been enabled
- You don't need to bother about any license validity for the database.
- Automatic backup is also taken care of.
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.
SSHinto your instance using your key pair for Linux user. For Windows, you can use SSH client such as putty to connect to your instance.
- We have to update and upgrade our Ubuntu to latest packages using the commands below
sudo apt-get update && sudo apt-get upgrade
- Install PostgreSQL by running the command below
sudo apt-get install postgresql
- To check the version of PostgreSQL installed run
- We are going to edit the PostgreSQL configuration file to change the address. To edit the configuration file (
sudo vim /etc/postgresql/9.5/main/pg_hba.conf.
9.5is the version of PostgreSQL installed. As at the time of writing this article, 9.5 is the default installation for PostgreSQL on Ubuntu OS.
- After opening our
pg_hba.confin vim, you can use any editor of your choice. Update the file which read something like this by 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
This would enable us to connect to our database instance.
# 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
- We need to update the
postgresql.conffile to enable remote connection from anywhere(IP address). To edit
sudo vim /etc/postgresql/9.5/main/postgresql.conf. Use
:set numberto enable the line numbers and look for line 59 which should be like this
Update it to this
#listen_addresses = 'localhost'
listen_addresses = '*'
- To make an effect on the changes made, we need to restart the PostgreSQL service. We can do that by running the command below
sudo /etc/init.d/postgresql restart
- Now that our PostgreSQL database is ready, we can log into PostgreSQL and create a username and a database.
sudo - su postgres psql
psqlcommand gives us access to the Postgres interactive terminal
- Create a username by running the command below
CREATE USER ubuntu_user SUPERUSER;
- We have to create a password for the user with the command below
ALTER USER power_user WITH PASSWORD 'password';
- To create a database with the user created, run
CREATE DATABASE mydatabase WITH OWNER power_user;
- To exit from psql shell, run
\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.
Connect to The Database via Database client
We would be using a database client called postico. you can use any database client of your choices.
- Click on
new favouriteto add your connection parameters
- Fill in the field box The field parameters in the screenshot above are explained below
nicknamefield can be anything
Hostfield contains the public IP address of our ECS instance
Userfield contains the username we created earlier which is
passwordfield is for the password we created for the
- The database field is for the database we created earlier which is
Once you have successfully connected to your database instance, you should see the database we created. From the database client, you can perform basic CRUD(Create, Read, Update and Delete) operations. Irrespective of the database client you are using, the parameters would always be the same.
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.
Published at DZone with permission of Kehinde Sanni , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.