📕
SEIRFX
  • Introduction
  • About These Notes
  • Schedule
  • Unit 2
    • Node
      • Internet Fundamentals
      • Full-Stack Fundamentals
      • Intro to Node
      • Node Modules
      • Node Packages
    • Express
      • Intro to Express
      • Routes
      • Routes Lab
      • Views
      • Templates
      • Layouts & Controllers
    • CRUD & REST
      • GET & POST
      • GET & POST Lab
      • PUT & DELETE
    • API Calls in Express
      • Axios
      • Request (no longer maintained)
    • Sequelize
      • Terminology
      • Setup
      • Using Models
      • Seeding Data
      • Validations and Migrations
      • Resources
      • 1:M Relationships
      • N:M Relationships
    • Express Authentication
      • Research Components
      • Code Components
      • Auth in Theory
        • Sessions
        • Passwords
        • Middleware
        • Hooks
      • Auth in Practice
        • Create the User
        • User Signup
        • Sessions
        • User Login
        • Authorization and Flash messages
  • Development Workflow
    • Command Line
      • The Terminal
      • Filesystem Navigation
      • File Manipulation
      • Additional Topics
    • Intro to Git
      • Version Control
      • Local Git
      • Remote Git
      • Git Recipes
    • Group Collaboration
      • Git Workflows
      • Project Roles and Tools
    • VS Code Tips & Tricks
  • HTML/CSS
    • HTML
    • CSS Selectors
    • CSS Box Model and Positioning
      • Box Model
      • Display and Positioning
      • Flexbox
      • Grid
      • Flexbox & Grid Games
      • Floats and Clears
      • Additional Topics
    • Advanced CSS
      • Responsive Design
      • Pseudo-Classes/Elements
      • Vendor Prefixes
      • Custom Properties
      • Additional Topics
    • Bootstrap
    • CSS Frameworks
    • Accessibility
  • JavaScript
    • Primitives
    • Arrays
    • Objects
      • Objects Lesson
      • Objects quick guide
      • Object-ception
    • Control Flow
      • Boolean Expressions
      • Conditionals
      • Loops
      • Promises
    • Functions
      • Scope
      • Callbacks
      • Higher Order Functions
      • Callbacks Review Lab
      • Timing Functions
      • Iterators
      • Combining Data Types
      • Combining Data Types Lab
    • Javascript in the browser
      • DOM and Events
      • DOM Manipulation
      • DOM Review
      • DOM Review Lab
      • HP DOM Lab
      • Programmatic DOM Manipulation
      • Grids & Pyramids
      • DOM & Data
      • DOM Events
      • Color Palette Picker
      • Sketchpad
    • HTML5 Canvas
    • How To Reduce Redundancy
    • OOP
      • Westworld Lab
      • OOP Factories
      • OOP Inheritance
      • OOP Inheritance Lab
      • Tomagotchi Lab
      • OOP Space Battle
      • OOP Snowman
      • (2019) JavaScript OOP
      • (2016) OOP with Classes
      • (1995) OOP with Prototypes
      • Constructors
      • Prototypes
    • Intro to TDD
    • Scoping
    • Inheritance
      • Prototypal Inheritance
      • Call, Apply, and other Functions
      • ES6 Inheritance
      • Resources
    • Custom Node Modules
    • Additional Topics
      • AJAX, Fetch, and Async/Await
      • AJAX w/JSON and Localstorage
        • AJAX w/JSON
        • Local Storage
      • Async module
      • Data Scraping
  • jQuery
    • Intro
      • DOM Manipulation
      • Reddit Practice
      • Styling
      • Events
    • Plugins
    • AJAX
  • APIs
    • Fetch
    • AJAX w/jQuery
    • AJAX w/Fetch
  • Databases
    • Intro to SQL
    • Advanced SQL
    • MongoDB
      • Intro to NoSQL
      • CRUD in MongoDB
      • Data Modeling
      • Intermediate Mongo
  • Left over Node/Express
    • Testing with Mocha and Chai
    • Mongoose
      • Mongoose Associations
    • JSON Web Tokens
      • Codealong
    • Additional Topics
      • oAuth
      • Geocoding with Mapbox
      • Geocoding and Google Maps
      • Cloudinary
      • Websockets with Socket.io
      • SASS
  • Ruby
    • Intro to Ruby
    • Ruby Exercises
    • Ruby Classes
    • Ruby Testing with Rspec
    • Ruby Inheritance
    • Ruby Data Scraping
  • Ruby on Rails
    • Intro to Rails
    • APIs with Rails
    • Asset Pipeline
    • Rails Auth and 1-M
      • Auth Components
    • Rails N:M
    • ActiveRecord Polymorphism
    • Additional Topics
      • oAuth
      • SASS
      • Rails Mailers
      • Cloudinary
      • Jekyll
  • React (Updated 2019)
    • ES6+/ESNext
      • Const and Let
      • Arrow Functions
      • Object Literals and String Interpolation
      • ES6 Recap
      • ES6 Activity
    • Intro to React
      • Create React App
      • Components and JSX
      • Virtual DOM
      • Props
      • Dino Blog Activity
      • Nested Components
      • Lab: LotR
    • React State
      • Code-Along: Edit Dino Blog
      • Lab: Simple Calc
      • Lifting State
    • React Router
      • Browser History/SPAs
      • React Router (lesson and full codealong)
      • Router Lab
    • Fetch and APIs
      • APIs with Fetch and Axios
      • Fetch the Weather
    • React Hooks
    • React LifeCycle
      • Lab: Component LifeCycle
    • React Deployment
    • Additional Topics
      • React Frameworks
        • Material UI Theming
      • Typescript
        • More Types and Syntax
        • Tsconfig and Declaration Files
        • Generics with Linked List
      • Redux
      • TypeScript
      • Context API
      • React Native
  • Meteor
  • Deployment and Config
    • Installfest
      • Mac OSX
      • Linux
      • Git Configuration
      • Sublime Packages
    • Deploy - Github Pages
    • Deploy - Node/Sequelize
    • Deploy - Node/MongoDB
    • Deploy React
    • Deploy - Rails
      • Foreman (Environment Variables)
    • Deploy - AWS Elastic Beanstalk
    • Deploy - S3 Static Sites
    • Deploy - Django
    • Deploy - Flask
  • Data Structures and Algorithms
    • Recursion
    • Problem Solving - Array Flatten
    • Binary Search
    • Algorithm Complexity
    • Stacks and Queues
    • Bracket Matching
    • Ruby Linked Lists
      • Sample Code
      • Beginner Exercises
      • Advanced Exercises
    • JS Linked Lists
      • Sample Code
      • Beginner Exercises
      • Beginner Solutions
    • Hash Tables
    • Intro to Sorting
    • Insertion Sort
    • Bucket Sort
    • Bubble Sort
    • Merge Sort
    • Quick Sort
    • Heap Sort
    • Sorting Wrapup
    • Hashmaps
    • Trees and Other Topics
  • Python
    • Python Installation
    • Intro to Python
    • Python Lists
    • Python Loops
    • Python Dictionaries
    • Python Sets and Tuples
    • Python Cheatsheet
    • Python Functions
    • Python Classes
    • Python Class Inheritance
    • Intro to Flask
    • Intro to SQLAlchemy
      • Flask and SQLAlchemy
    • Using PyMongo
    • Intro to Django
    • CatCollector CodeAlong
      • URLs, Views, Templates
      • Models, Migrations
      • Model Form CRUD
      • One-to-Many Relations
      • Many-to-Many Relations
      • Django Auth
    • Django Cheatsheet
    • Django Auth
    • Django Polls App Tutorial
    • Django School Tool Tutorial
    • Django 1:M Relationships
    • Custom Admin Views
    • Data Structures and Algorithms
      • Recursion
      • Binary Search
      • Stacks and Queues
      • Linked Lists
      • Binary Trees
      • Bubble Sort
      • TensorFlow & Neural Networks
    • Adjacent Topics
      • Raspberry Pi
      • Scripting
  • Assorted Topics
    • History of Computer Science
    • Regular Expressions
    • Being Successful in SEI
    • Internet Fundamentals
      • Internet Lab
    • Adjacent Workflow
      • UX/UI
      • Wireframing Exercise: Build an Idea
      • Agile
    • Post SEI
      • Learning Resources
      • Deliverables -> Portfolio
      • FAQ
  • Projects
    • Project 1
    • Project 2
    • Project 3
      • Project 3 Pitch Guidelines
    • Project 4
    • Past Projects
      • Project 1
      • Project 2
      • Project 3
      • Project 4
      • Portfolios
    • Post Project 2
    • MEAN Hackathon
      • Part 1: APIs
      • Part 2: Angular
    • Portfolio
  • Web Development Trends
  • Resources
    • APIs and Data
    • Tech Websites
    • PostgreSQL Cheat Sheet
    • Sequelize Cheat Sheet
    • Database Administration
  • Archived Section
    • (Archived) ReactJS
      • Intro to React
        • Todo List Codealong
        • Additional Topics
      • Deploy React
      • React with Gulp and Browserify
        • Setting up Gulp
        • Additional Gulp Tasks
      • React Router
        • OMDB Router
        • OMDB Search
        • Additional Resources
      • React Animations
        • CSS Animations
    • AngularJS
      • Intro to AngularJS
        • Components and SPA
        • Create an Angular App
      • Angular Directives and Filters
      • Angular Animation
      • Angular Bootstrap Directives
        • Bootstrap Modals
      • Angular $http
      • Angular Services
        • Service Recipes
        • ngResource
        • Star Wars Codealong
      • Angular Routing
      • Angular + Express
      • Angular Authentication
        • Additional Topics
      • Angular Components
      • Angular Custom Filters
      • Angular Custom Directives
Powered by GitBook
On this page
  • Postgres - Local
  • Backup postgres database to file
  • Restore postgres file to database
  • Reset local database
  • Postgres - Heroku
  • Push existing .sql file to heroku
  • Push / Pull database to / from Heroku
  • Push database to heroku
  • Pull database from heroku
  • Reset database
  • Mongo - Local
  • Backup mongo database to file
  • restore mongo database from file
  • Mongo - Heroku
  • Push existing dump to heroku
  • Production Database Access
  • Postgres
  • Mongo

Was this helpful?

  1. Resources

Database Administration

This guide explains the following:

  • Postgres - Local - backup / restore

  • Postgres - Heroku - backup / restore

  • Mongo - Local - backup / restore

  • Mongo - Heroku - backup / restore

  • Accessing production databases

    • Mongo / Postgres GUI options

IMPORTANT NOTE: This document is currently in beta, but should give you a good starting point for backing up data, restoring data, and accessing production databases.

Postgres - Local

Remember you need to have your local postgres server (the elephant icon on mac) running for any of these commands to work.

Backup postgres database to file

This will create a file named db.sql which will contain all of the data of the local postgres database named APP_DATABASE_NAME change these names to match your app as needed.

This is useful for sending to your teammates if you're collaborating or just to have a backup incase you break something.

pg_dump -s -c APP_DATABASE_NAME > db.sql

The -s flag is for schema only. If you want your development data included just remove that flag.

The -c flag tells it to drop old data before creating new data. This is just incase you've done this more than once and are overwritting existing data.

Restore postgres file to database

This will create a database named APP_DATABASE_NAME using the data in db.sql. It will DESTROY the database if it already exists.

psql -d APP_DATABASE_NAME < db.sql

This will throw some errors if the dump was created by someone else. It is ok to ignore these errors.

Reset local database

For the restore process to work you need to have an empty database. The easiest way to achieve this is to just drop and re-create the database.

Make sure there is nothing actively connected to the database (pgCommander, rails, node, etc) when you do this or it won't work.

Delete the database

dropdb APP_DATABASE_NAME

Create a new empty database

createdb APP_DATABASE_NAME

Postgres - Heroku

Push existing .sql file to heroku

If you already have a .sql file you want to upload to heroku you can use this command.

heroku pg:psql < db.sql

Push / Pull database to / from Heroku

Heroku also has commands that will allow you to transfer your local database to heroku or vise-versa without needing to create a .sql file.

Push database to heroku

Uploads the local database named APP_DATABASE_NAME to heroku using the heroku config value for DATABASE_URL.

heroku pg:push APP_DATABASE_NAME DATABASE_URL

Pull database from heroku

This will download the database using the heroku config value for DATABASE_URL and store it in the database named APP_DATABASE_NAME.

heroku pg:pull DATABASE_URL APP_DATABASE_NAME

Reset database

Before you can push a database to heroku you must clear the old database (if one exists).

heroku pg:reset DATABASE_URL

Mongo - Local

Remember you need to have mongod running to do any database manipulation.

Backup mongo database to file

This will create a folder called dump and backup the contents of the mongo database APP_DATABASE_NAME in that folder.

mongodump -h 127.0.0.1 -d APP_DATABASE_NAME -o dump/

restore mongo database from file

This will create a mongo database named APP_DATABASE_NAME by using the dump named APP_DATABASE_NAME in the dump directory.

mongorestore -h 127.0.0.1 -d APP_DATABASE_NAME dump/APP_DATABASE_NAME
  • -h set host to local host.

  • -d target database name (database to create)

  • dump/APP_DATABASE_NAME source dump file to import (listed as last parameter)

Mongo - Heroku

Push existing dump to heroku

mongorestore -h xyz.mongolab.com:12345 -d remote_db_name -u username -p password dump/mylocal_db_name/

you can get the details for mongorestore from the mongo connection uri by running heroku config to get the mongo connection uri. It will be in the followin format.

MONGOLAB_URI: mongodb://username:password@server:port/remote_db_name

# which will look kinda like this:

MONGOLAB_URI: mongodb://heroku_abcdefg:abcdefghijklm@xyz.mongolab.com:12345/heroku-13411

Production Database Access

Postgres

Your local database is only for local testing. Your production database is hosted by heroku so if we want to see what is going on in the database we need to connect to it. To get the database connection details run heroku config and copy the database connection string (starts with "postgres://"). With that url in your clipboard load PG Commander and click "New Favorite" and it will automatically populate the connection fields.

If you aren't using PG Commander you'll have to manually enter the parts of the connection string. The format is as follows:

postgres:// USER_NAME : PASSWORD @ SERVER_URL : PORT_NUMBER / DATABASE_NAME

It'll look kinda like this (heroku passwords tend to contain a hyphen):
postgres://xxxxxx:yyyyyyy-zzzzzzzz@ec2-54-83-55-214.compute-1.amazonaws.com:5432/abcdefghijklmnop

Postgres GUIs

Mongo

The steps for mongo are the same except the url will start with mongodb:// and you'll be setting it up in your mongo gui instead of PG Commander.

You'll need to click "create" to create a new connection and manually enter the connection details from heroku config which will be in the following format.

MONGOLAB_URI: mongodb://username:password@server:port/remote_db_name

# which will look kinda like this:

MONGOLAB_URI: mongodb://heroku_abcdefg:abcdefghijklm@xyz.mongolab.com:12345/heroku-13411

Mongo GUIs:

PreviousSequelize Cheat SheetNextArchived Section

Last updated 4 years ago

Was this helpful?

PG Commander
PG Admin
RoboMongo
Mongo Hub