![]() ![]() Import dotenv from dotenv and load it using nfig() - what this does is to search for.You can also make use of their Client API to connect to the DB. ![]() Pool is the easiest and common way of using pg. We use this to connect to our PostgreSQL Db. Check pg documentation to read more on pooling. ![]() This is where we will start making use of pg package with basic SQL query to create the table.Ĭopy and paste the following into db.js in the project directory we need to create reflections table in our DB. If you use cloud PostgreSQL DB, copy the URL and use it instead. Your Database URL should be in the following you were unable to set your DB locally, the easy way out is to make use of any PostgreSQL cloud DB such as ElephantSQL. Use \l to check the list of available DB from the terminal $ psqlįirst, let save our Database URL in the system environment. To create the DB, we can make use of any PostgreSQL client such as POSTICO and we can also create the DB directly from the terminal using createdb command. Let create our DB and name it reflection_db. NODEJS API AWS POSTGRESQL INSTALLRun the following command to install the two packages $ npm install -save pg dotenv Lastly, we need to install PG and dotenv(we need this to load our environment variables from. Your new project structure should look like the following project ![]() Create Reflection.js js file in controller folder and index.js in db folder. Next, create two new folders inside usingDB - controllers and db. Move the previous folders in src to usingJSObject folder. Create two new folders in src - usingJSObject and usingDB. Now, let's make some tweak to our project structure. To check if progress was installed successfully, run which psql from the terminal Using PG alone will also give us the opportunity to understand some basic SQL queries as we will be querying and manipulating data in the DB using raw SQL queries. Instead of using an ORM, we will use PG NodeJS package directly - PG is a NodeJs package for interfacing with the PostgreSQL database. We have several ORM packages that could be used with our database e.g Sequelize. ORM packages expose some methods needed to query and manipulate the data in the database. ORM is basically a technique that allows us to query and manipulate data from the database with little or zero knowledge of SQL. Check out this answer on stackoverflow to get a better understanding of ORM. To make this simple and so that we can have a better understanding of how SQL works, I'm not going to make use ORM(Object Relational Mapping) in this post. SQL is basically the only language you can use in querying data in the database. SQL is a standard language for storing, manipulating and retrieving data in databases. NODEJS API AWS POSTGRESQL HOW TOWe will also learn how to write some basic (Structure Query Language) queries. In this post, I'll explain how to connect and use PostgreSQL Database to store input data as opposed to JavaScript Object datatype we used in part 1. Click here to read my post on Building a simple API with Nodejs, Expressjs, and JS Data structure to persist data. If you have not read my previous post, please do. This tutorial is a continuation from where we stopped from the last post. "There is always one more bug to fix." Ellen Ullman ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |