Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
NotificationsYou must be signed in to change notification settings

mongodb-developer/laravel-postgresql-to-mongodb

Repository files navigation

Laravel Logo

PostgreSQL   ➜  MongoDB

Laravel Postgres to MongoDB Migration

This repo demonstrates how easy it is to migrate a Laravel application from a relational database (Postgres) to MongoDB.

The main branch of this repository contains a sample Laravel application that uses Postgres as its database, and ElasticSearch for full text search.

Themongodb branch contains the same application, but it has been modified to use MongoDB Atlas as its database and Atlas Search for full text search.

Prerequisites

  • PHP 8.2 or higher
  • Composer
  • Postgres
  • MongoDB
  • Laravel 12.x
  • Laravel MongoDB package (jenssegers/mongodb)
  • Laravel UI package (laravel/ui)
  • Node.js and npm (for frontend scaffolding)
  • Git
  • A code editor of your choice (e.g., VSCode, PHPStorm)
  • Basic knowledge of Laravel, Postgres, and MongoDB
  • Docker (for simplified setup)
  • (Optional) For manual setup, installPostgreSQL Server locally.
  • MongoDB Server can be installed locally, or you can use the MongoDB cloud serviceMongoDB Atlas.

Installation and Setup

Tutorial:https://dev.to/mongodb/migrating-a-laravel-application-from-a-relational-database-postgresql-to-mongodb-m0m

Quick Setup with Docker

If you have Docker installed, you can get up and running quickly.

We have included adocker-compose.yml file to simplify the setup process. For the main branch, it sets up a PostgresDatabase, ElasticSearch container, a Laravel Queue Worker, and runs the laravel scout command to index the posts in elastic search, so you can test the app before we migrate to MongoDB.

Themongodb branch, has its owndocker-compose.yml file that sets up the application and a laravel queue worker. We are using MongoDB Atlas for the database in this branch, so no need of a container for MongoDB. You will however need to set up a free cluster on MongoDB Atlas and update the.env file with your connection string.

  1. Clone the repository:

     git clone git@github.com:mongodb-developer/laravel-postgresql-to-mongodb.gitcd laravel-postgresql-to-mongodb
  2. Copy the.env.example file to.env:

    cp .env.example .env
  3. Start the Docker containers:

    docker compose up -d --build

    The above command will build and start the application, Postgres database, and other necessary services in detached mode.The --build flag ensures that Docker images are rebuilt, which is necessary when switching between branches or when dependencies have changed.TheDockerfile.app will handle the installation of PHP dependencies, run and build frontend assets, set up the application, and run migrations and seeders.TheDockerfile.queue will set up a Laravel queue worker to handle any queued jobs.

  4. Run the following command to import posts and users into ElasticSearch (main branch only):

    docker composeexec app php artisan scout:queue-import"App\Models\Post"docker composeexec app php artisan scout:queue-import"App\Models\User"

    This command will index all existing posts into ElasticSearch for full text search functionality.

  5. Access the application:Open your browser and navigate tohttp://localhost:8080.

  6. You can log in with the following credentials:

    • Email:test@example.com
    • Password:password123
  7. To stop the application and remove containers, networks, and volumes, run:

    docker compose down -v

Manual Setup

If you prefer to set up without Docker:

  1. Clone the repository:
    git clone git@github.com:mongodb-developer/laravel-postgresql-to-mongodb.gitcd laravel-postgresql-to-mongodb
  2. Install dependencies:
    composer install
  3. Set up environment variables:
    cp .env.example .envphp artisan key:generate
  4. Configure database connection in .env:
    • For Postgres (main branch):
    DB_CONNECTION=pgsqlDB_HOST=127.0.0.1DB_PORT=5432DB_DATABASE=laravelDB_USERNAME=laravelDB_PASSWORD=secret
    • For MongoDB (mongodb branch):
    DB_CONNECTION=mongodbDB_HOST=DB_PORT=27017DB_DATABASE=laravelDB_USERNAME=DB_PASSWORD=
  5. Run migrations and seed the database:
    php artisan migrate --seed
  6. Batch import posts into ElasticSearch (main branch only):
    php artisan scout:import"App\Models\Post"
  7. Install and build frontend assets:
    npm installnpm run dev
  8. Serve the application:
    php artisan serve
  9. Access the application:Open your browser and navigate tohttp://localhost:8000.

Switching Between Postgres and MongoDB Versions

To switch between database versions:

  1. Checkout the desired branch:
    • For Postgres:
      git checkout main
    • For MongoDB:
      git checkout mongodb
  2. Follow the setup instructions for the chosen branch. Remember to:
    • Stop and remove existing containers: docker compose down -v
    • Rebuild and start containers: docker compose up -d --build
    • If you are following the manual process, run composer install after switching branches as dependencies may differ.
    • Update your .env file with appropriate database settings.
    • Clear configuration cache with php artisan config:clear
    • Run migrations again if needed (postgres main branch): php artisan migrate:fresh --seed

Application Features

A simple app ( users, blog posts, comments), full text search.

  • It allows us to demonstrate CRUD operations and relationships in both Postgres and MongoDB.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp