How to install PostgreSQL in AWL EC2 instance, import data and seed data

ehdtlaos

Edrick Ee

Posted on August 17, 2021

How to install PostgreSQL in AWL EC2 instance, import data and seed data

when EC2 has been created and you are inside of the instance

Access into instance: ssh -i *.pem ubuntu@YOURIPADDRESS

Update instance: sudo apt-get update

Upgrade instance: sudo apt-get -y upgrade

download PostgreSQL: sudo apt-get install postgresql postgresql-contrib​
a. in case of error message:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package postgresql-contrib
Enter fullscreen mode Exit fullscreen mode

b. do this

sudo apt-get install wget ca-certificates
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
Enter fullscreen mode Exit fullscreen mode

change your password: sudo -u postgres psql

postgres=#\password
Enter fullscreen mode Exit fullscreen mode

Update configuration for remote access for clients

sudo vim /etc/postgresql/13/main/pg_hba.conf
Enter fullscreen mode Exit fullscreen mode

13 is version of postgresql and it will change once it has new version.

b. Near bottom of file after local rules, add rule (allows remote access):

host all all 0.0.0.0/0 md5
Enter fullscreen mode Exit fullscreen mode

c. save & exit out from config

Edit config in vim

sudo vim /etc/postgresql/13/main/postgresql.conf
Enter fullscreen mode Exit fullscreen mode

b. Change line 59 to listen to external requests
- listen_address='*'
- save & exit out from config

Restart the Postgresql server

sudo /etc/init.d/postgresql restart
Enter fullscreen mode Exit fullscreen mode

Now you may connect to the EC2 postgresql using the public DNS provided and port 5432.

Create database

$sudo su postgres
$psql
postgres=# CREATE DATABASE <database_name>;
postgres=# CREATE USER <user> with encrypted password '<password>';
postgres=# GRANT ALL PRIVILEGES ON DATABASE <database_name> TO <user>;
Enter fullscreen mode Exit fullscreen mode

How to import data into EC2 instance

scp -i *.pem -r {foldername} ubuntu@ipaddress:~/yourfolderinUbuntu/
Enter fullscreen mode Exit fullscreen mode

How to import csvs into table

productdb=# \copy products from /home/ubuntu/foldername/filename.csv delimiter , csv header;
/products/csvs/product.csv: No such file or directory

//if you have null as 'null' or different name you can write
NULL 'null_name' after delimiter
Enter fullscreen mode Exit fullscreen mode

If you are wanting to get repo from github instead of making db, after update and upgrade:

installation:

curl -sL [https://deb.nodesource.com/setup_14.x](https://deb.nodesource.com/setup_14.x) | sudo -E bash
sudo apt-get install -y nodejs
sudo apt-get install git
Enter fullscreen mode Exit fullscreen mode

port-forwarding:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
Enter fullscreen mode Exit fullscreen mode

git clone your repo into it

install npm modules, run build scripts, run node

Link for detail instrusction: https://www.shubhamdipt.com/blog/postgresql-on-ec2-ubuntu-in-aws/
Link for error message fix: https://stackoverflow.com/questions/53434849/cannot-install-postgres-on-ubuntu-e-unable-to-locate-package-postgresql
Link for AWS EC2 creation: https://fern-kettle-978.notion.site/Daily-Reflection-8-4-f7bb9770bf70451abfc0dceb171a4b88
Link for seeding data: https://www.enterprisedb.com/postgres-tutorials/how-import-and-export-data-using-csv-files-postgresql

💖 💪 🙅 🚩
ehdtlaos
Edrick Ee

Posted on August 17, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related