Skip to content

Instantly share code, notes, and snippets.

@ericpkatz
Last active March 10, 2024 13:39
Show Gist options
  • Save ericpkatz/f19c34c14688302ee363cade014d57b9 to your computer and use it in GitHub Desktop.
Save ericpkatz/f19c34c14688302ee363cade014d57b9 to your computer and use it in GitHub Desktop.
Cloud 9 - Setting Up Full Stack Environment by installing postgres and setting up credentials with github

Setting up a AWS-Cloud9 Postgres Environment

go into sudo interactive mode (command #1)

sudo -i

install and configure postgres (command #2)

yum install postgresql15-server -y  && service postgresql initdb && service postgresql start
  • sometimes this command hangs and you'll see an error about a lock and yum. This usuallly resolves by waiting, or you can always do ctl-c to kill the command and try again. Sometimes it takes a few times.

log into postgres as postgres user (command #3)

sudo -u postgres psql

create ec2-user and ec2-user and database (command #4)

create user "ec2-user" createdb; create database "ec2-user";

quit postgres (command #5)

\q

configure postgres for access (command #6)

rm  -f /var/lib/pgsql/data/pg_hba.conf && echo "local   all     all trust">>/var/lib/pgsql/data/pg_hba.conf && echo "host    all     ec2-user     all trust">>/var/lib/pgsql/data/pg_hba.conf

restart postgres (command #7)

service postgresql restart && exit

NOTE: your environment will terminate when not in use, so when you log back in you probably want to start postgres

sudo -i
service postgresql start && exit

a few more commands to set up github credentials

Don't use this environment to store applications. If something is important, push it up to github. If it's not important, delete it. Plus it doesn't take much to setup a new environment.

create public private key pair command #8

hit enter for defaults

ssh-keygen -t ed25519 -C <your email surrounded by double quotes>

start ssh agent command #9

eval "$(ssh-agent -s)"

add your key to agent command #10

ssh-add ~/.ssh/id_ed25519

see your public key command #11

cat ~/.ssh/id_ed25519.pub

copy your public key command #12

  • copy contents of file (example below)
ssh-ed25519 some-long string etccc.. youremail@email.com

share your public key with github command #13

  • create .ssh key with this value on github.com
https://github.com/settings/ssh/new

set your username for your git commits #14

git config --global user.name "FIRST_NAME LAST_NAME"

you can create a quick app with the code below

createdb acme_db && mkdir app && cd $_ && npm init -y && npm i pg express && touch server.js

paste code in server.js

const pg = require('pg');
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;

const client = new pg.Client('postgres://localhost/acme_db');
client.connect();

client.query(`
    DROP TABLE IF EXISTS users;
    CREATE TABLE users(
     name VARCHAR(255)
    );
  
`)
  .then(()=> {
      return Promise.all([
          client.query('insert into users(name) values($1)', ['moe']),
          client.query('insert into users(name) values($1)', ['lucy'])
      ]);
  })
  .then(()=> {
    app.listen(port, ()=> console.log(`listening on port ${port}`));
  });


app.get('/', (req, res, next)=> {
    client.query('select * from users;')
    .then( response => res.send(response.rows))
    .catch(next);
});

run app

node server.js
  • click preview application button
  • you should see an api call to your application which shows users

next steps

  • take app you created in previous steps
  • create a local github repo
  • create a repo on github
  • push local github repo to github
  • when pushing for first time you will be prompted about using new key, hit enter (default)

results

  • any code you create you can push to github where you can always access it
  • you can delete your cloud 9 environment anytime you want and always recreate it!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment