Bootcamp
Search…
3.POCE.10: EC2 Deployment with Postgres

Get an EC2 Instance

Use an old Amazon EC2 Instance or create a new one following instructions in 2.11: AWS Deployment.

Update Postgres Connection Configs for Remote Deployment

We need to run our servers on port 80 for HTTP. Alter the PORT variable to change according to process.argv.
index.js
1
const PORT = process.argv[2];
2
​
3
// ...
Copied!
The following should run without errors on the command line.
1
node index.js 3004
Copied!
Alter our Postgres connection configs to include user and password attributes.
index.js
1
// create separate DB connection configs for production vs non-production environments.
2
// ensure our server still works on our local machines.
3
let pgConnectionConfigs;
4
if (process.env.ENV === 'PRODUCTION') {
5
// determine how we connect to the remote Postgres server
6
pgConnectionConfigs = {
7
user: 'postgres',
8
// set DB_PASSWORD as an environment variable for security.
9
password: process.env.DB_PASSWORD,
10
host: 'localhost',
11
database: 'birding',
12
port: 5432,
13
};
14
} else {
15
// determine how we connect to the local Postgres server
16
pgConnectionConfigs = {
17
user: '<MY_UNIX_USERNAME>',
18
host: 'localhost',
19
database: 'birding',
20
port: 5432,
21
};
22
}
23
​
24
// ...
Copied!
Commit and push the changes to GitHub. We will clone this repo with the latest changes to our EC2 instance below.

Install and Configure Postgres on EC2

Log into our EC2 instance using SSH. See 2.11: AWS Deployment for a refresher.
Update the apt system and install some common libraries
1
sudo apt update
2
sudo apt upgrade
3
sudo apt install build-essential
Copied!
Install Postgres on our EC2 instance with the following commands
1
sudo apt install postgresql
2
sudo apt install postgresql-client
Copied!
Note that when these packages install, a user named postgres also gets created _in ubuntu. _Postgres SQL server system has a system of users and logins for the server itself. By default the configuration expects that this is one-to-one with the Ubuntu system users as well; i.e., if a user inside the Postgres system exists and wants to use the database, it is expected by default that there is also an Ubuntu user of the same name.
Start the Postgres server in the background.
1
sudo service postgresql start
Copied!
Execute psql as the (Postgres server) default postgres user.
1
sudo -u postgres psql
Copied!
Set a password for the postgres user. Record the password so that we can assign it to an environment variable later.
Command Template
1
ALTER USER postgres PASSWORD '<MY_SECRET_PASSWORD>';
Copied!
Sample Command
1
ALTER USER postgres PASSWORD 'newPassword';
Copied!
Exit psql and restart Postgres to ensure the password change gets propagated for new connections.
1
sudo service postgresql restart
Copied!
Log out of the postgres user.
1
exit
Copied!

Clone Bird Watcher Repo to EC2

Clone the Bird Watcher repo onto our EC2 instance and cd into the repo folder.
1
git clone <BIRDING_REPO_CLONE_URL>
2
cd <MY_REPO_FOLDER_NAME>
Copied!
Change the user to the one with the correct database permissions. sudo su - explanation here.
1
sudo su - postgres
Copied!
Create your database.
1
createdb birding
Copied!
sudo su - postgres changes the user and therefore changes the directory. Note the directory with your repository code and run the SQL to create your tables.
1
psql -d birding -f <ABSOLUTE_PATH_TO_REPO>/init_tables.sql
Copied!
-d stands for database. -f stands for file path. In the following code, we assume we have a file init_tables.sql that contains all the queries we need to set up our database schema. If we don't have this file already, create one locally, test it, commit, and push it to GitHub before pulling it from our EC2 machine.
Test that our tables were created successfully with psql.
Change back to the default ubuntu user.
1
exit
Copied!

Run Bird Watcher

Run our server, setting DB_PASSWORD and ENV environment variables for our Postgres connection configs. Replace <MY_SECRET_PASSWORD> with the DB password we set above.
1
sudo DB_PASSWORD='<MY_SECRET_PASSWORD>' ENV='PRODUCTION' node index.js 80
Copied!
There could be errors if your app expects data to be in the DB. If you need such data, write queries to insert it locally, save those queries to a SQL file, commit and push those changes to GitHub, pull those changes to EC2, execute the commands in psql, and restart the server.