Laravel Database Connections

Following are some database connection possibilities. It's not mandatory to use one of them but given for demo purpose.


For MySQL

Copy file .env.example, and change its name to .env. Then in file .env complete this database configuration:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

For SQLite

Create a database in database folder.

#create database
touch database/database.sqlite

Copy file .env.example, and change its name to .env. Then in file .env replace this database configuration:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

To this:

DB_CONNECTION=sqlite
DB_DATABASE=/path_to_your_project/database/database.sqlite

For PostgreSQL

  1. Install PostgreSQL: You can refer its official documentation to install it.
  2. You have to create a user to interact with the PostgreSQL database..
  3. $ sudo -u postgres createuser --interactive
    enter name of role to add: laravel
    shall the new role be a superuser (y/n) n
    shall the new role be allowed to create database (y/n) n
    shall the new role be allowed to create more new roles (y/n) n
  4. You have to update/change the default password with your desired one. In our case, we have put 'password' as a password.
  5. $ sudo -u postgres psql
    postgres= ALTER USER laravel WITH ENCRYPTED PASSWORD 'password';
    postgres= \q
  6. Create database for your Laravel app.
  7. sudo -u postgres createdb laravel
  8. Copy file .env.example, and change its name to .env. Then in file .env replace this database configuration:
  9. DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=laravel
    DB_USERNAME=root
    DB_PASSWORD=
  10. To this:
  11. DB_CONNECTION=pgsql
    DB_HOST=127.0.0.1
    DB_PORT=5432
    DB_DATABASE=laravel
    DB_USERNAME=laravel
    DB_PASSWORD=password
© 2017- Pixinvent, Hand-crafted & Made with ❤️