Solved! How to Import Data from SQL file into Postgres Database through Bash Command

Solved! How to Import Data from SQL file into Postgres Database through Bash Command

    When setting up a Ruby on Rails application, you might want to use Postgres as a backend Database. If you decide to go with Postgres database to store your data then knowing how to seed your database with actual important data (not dummy data used for testing) is crucial not only for testing purposes but also for production. 

    Full Stack Developers should know how important it is to know how to migrate data from one database on one server to another server located on a different machine. One way to do this is by dumping all your databases into a single SQL File then importing the data into a Postgres Database through a command prompt/console or a GUI.

    Command/Shell/Console is the best tool to use when seeding the database because the process goes so fast and efficient. Another advantage of using the Command Prompt or Console to migrate the database is that it allows developers to load large SQL files (Petabytes of data ) containing data for migration. Unlike other ways of importing data such as using a GUI version, the capabilities might be limited. 

    Why would one want to use the command to migrate data into a newly created database? one might ask: Well, picture this, a new hire is brought on board as a Web Developer, Full Stack Developer or a Software Engineer,  in most cases it is required to load the application you will spend most of the time developing. This application has to be loaded on the local machine so that development can take place. This process requires creating databases (if the application uses one) and connecting all the bits and bolts together, this is when data migration becomes important for a user to sign in and other functionalities to work. It is important to have this kind of tooling in your toolbox to better do the job.

    Below are the commands that allow seeding an already existing database with data so that a Ruby on Rails or any other Application can function as expected.
  1. Make sure that the Database Schema is created in Postgres Database Engine
  2. If you are using a Linux Subsystem on Windows 10, then start Bash Console in Administrative mode
  3. Make a Postgres System User Name (this user name is used by the System to start a Postgres Database Engine and the Database Server) which is "posgres" as a Superuser here is the command:
    su -u postgres -i (This is not recommended for production, instead give minimal privileges to the posgres user so if it ever gets compromised, the intruder will not be able to propagate to other subsystems.)
  4. Then run this command:
    psql theDatabasenameYouWantToPopulateWithDatab
  5. Then hit enter, if everything goes well, the data will be migrated.