I'm using PostgreSQL and want to try Laravel.
First - my up() function:
public function up()
{
Schema::create('entries.entries', function($t) {
$t->increments('id');
$t->string('username', 50);
$t->string('email', 100);
$t->text('comment');
$t->timestamps();
});
}
And i have two questions:
1) I haven't shema entries in my database, so, how can i change my up function to create it too? I dont want to do it manually.
2) I got an error when executed migration:
sudo php artisan migrate
[Illuminate\Database\QueryException]
SQLSTATE[3F000]: Invalid schema name: 7 ERROR: no schema has been selected
to create in (SQL: create table "emigrations" ("migration" varchar(255) not null,
"batch" integer not null))
How can i fix it?
Well i know that this question has more than 2 year's... but for the future people that reaches this. Laravel does not recognize the default 'public' schema of postgres by default and that's why you get the:
[Illuminate\Database\QueryException]
SQLSTATE[3F000]: Invalid schema name: 7 ERROR: no schema has been selected
to create in (SQL: create table "emigrations" ("migration" varchar(255) not null,
"batch" integer not null))
Thus to solve this error you just rename your Schema in postgres to anything besides public and, in your laravel project in the file config/database.php change the name of your pgsql conection schema to the one you want to use in your DB.
This should solve it.
It did for me anyways...
1) I don't think there is such functionality in Laravel but you can use an external package like https://github.com/pacuna/Laravel-PGSchema
2) Did you remove the default schema in your app/config/database.php file?
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'forge',
'username' => 'forge',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
Laravel and PostgreSQL DB Connection, Authentication, Migration:
Prerequisites*
PHP 7.2+
Composer (a dependency manager for PHP)
PostgreSQL 9.5+
Installing Laravel
Create a new project with auth scaffolding.
laravel new blog --auth
Now Change default db connection in blog\config\database.php
'default' => env('DB_CONNECTION', 'mysql'),
change to
'default' => env('DB_CONNECTION', 'pgsql'),
Next, we’ll need to configure our database credentials in Laravel by updating the .env file:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=projectmanager
DB_USERNAME=postgres
DB_PASSWORD=1234
Finally, we have to enable the PostgreSQL driver in our php.ini file. Let’s go into our XAMPP/WAMP directory and modify this file by commenting out these two drivers:
- extension=pdo_pgsql
- extension=pgsql
Note: check php.ini file location is bin\php\php7.3.5. Also, check the php.ini file is PHP or apache file.
Then Run migration for creating the table.
php artisan migrate
Now you get your final output.
Thanks and best of luck.
Related
I'm using Laravel 5 but I'm not being able to migrate my database table. I have a macbook pro and I'm using Terminal. I'm using php artisan command:
php artisan migrate.
When I execute this command, I get the following error message: [PDOException]
SQLSTATE[HY000] [2002] Connection refused.
I have configured my database.php following the official tutorial videos on laracasts.com. My database.php file looks like the following:
...
'fetch' => PDO::FETCH_CLASS,
...
'default' => env('DB_CONNECTION', 'sqlite'),
...
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
....
I have read many comments on stackoverflow.com about this issue. Most of them are talking about modifying the ".env" file. The thing is I can't find this file! Which makes me wonder if my installation of Laravel is complete or not! I read that my ".env" file might be overriding my "database.php" file but I can't file the ".env" file!
The env file is most likely hidden.
Run defaults write com.apple.finder AppleShowAllFiles YES and Killall Finder in Terminal.
Open Finder and you should be able to see the .env file.
Edit your .env file.
I'm trying to use artisan migrate to create tables in sqlite.
I have the following in database.php
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
and this is my migrate class up function
Schema::connection('sqlite')->create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Note:- I have set DB_CONNECTION to be sqlite via the environment variable.
On the command line I get nothing to migrate, and no db is created (also no table).
Any ideas how I can get artisan migrate to create an sqlite db in laravel 5?
I have no problem creating mysql tables via artisan.
Thanks
its because you didnt create the db. use touch command like this to create new sqlite database.
touch database/database.sqlite
then configure the environment variable like this
DB_CONNECTION=sqlite
DB_DATABASE=/absolute/path/to/database.sqlite
In order to answer this part of the OP's question:
"Any ideas how I can get artisan migrate to create an sqlite db in laravel 5?"
In some cases where deployment and migrations need to be scripted I use a Service Provider to check the Sqlite DB exists and if not create it.
Here are the steps:
1) Create a Provider and save it to app/Providers/SqliteServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class SqliteServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
$databaseFile = config('database.connections.sqlite.database');
if (!file_exists($databaseFile)) {
info('Make Sqlite File "' . $databaseFile . '"');
file_put_contents($databaseFile, '');
}
}
}
2) Register the provider in config/app.php. For example in a fresh L5.8 install the SqliteServiceProvider would be placed under the RouteServiceProvider like so:
'providers' => [
//...
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\SqliteServiceProvider::class, // <--- HERE
],
3) Add/replace the following to the 'connections' element of config/database.php
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_SQLITE_FILEPATH', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
4) In my .env file I add this:
DB_SQLITE_FILEPATH=/full/path/to/mysqlitefilename.sqlite
...but if you prefer you can skip this step and the DB will be created as database/database.sqlite.
5) Run your migration as usual and the sqlite file should be created then populated.
Ok I got it. artisan migrate seems to ignore the env DB_CONNECTION and the database.php default (which reads the env anyway). Instead it like the .env file DB_CONNECTION, with this set to sqlite it works (plus, as mentioned above you do need to manually create the database file, which is a pain for making dbs on the fly.
Also means you can't really change artisan migrate database types (ie: sqlite and mysql) on the fly as you need to change the .env variable each time you change the target db type.
go laravel.
That's right, you need to create empty database.sqlite file.
You should also use artisan --env parameter if you want artisan to load the concrete .env file. For example, artisan --env=testing migrate will force artisan to load .env.testing file.
I'm trying to get started with Laravel + PostgreSQL and been following the database tutorial.
Unfortunately, after updating the database configuration file and running php artisan migrate, the following error appears:
[InvalidArgumentException]
Database [postgres] not configured.
What puzzles me is that I didn't specify the "postgres" database in the configuration, but another database I set through cPanel, say "example_database".
Here's some relevant parts of my /config/database.php configuration:
'default' => env('DB_CONNECTION', 'postgres')
And inside the connections array of the same file:
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'example_database'), // This seems to be ignored
'username' => env('DB_USERNAME', 'example_username'),
'password' => env('DB_PASSWORD', 'example_password'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public'
],
The actual database credentials I'm using are working perfectly on my SQL Workbench client, so this seems to be a Laravel config problem. Any ideas? I have searched around for at least an hour to no avail.
You have to enter your configuration in the .env file.
The configuration you made will only be loaded if they are not already defined in .env
You need to use pgsql instead of postgres.
DB_CONNECTION=pgsql
DB_HOST=localhost
DB_DATABASE=DB_NAME
DB_USERNAME=USER
DB_PASSWORD=PW
Laravel sometimes caches your configurations. If you run into this problem while everything looks alright try running
php artisan config:cache
I know this is an old question and it already has an answer; however, here is an small explanation why:
If you check your database.php in the config directory, you will see that you have few connections types, including pgsql. So, the key have to match to the DB_CONNECTION in .env file. You can definitely replace pqsql connection key with postgres, and it will work on the same way.
However, I would recommend replacing the value DB_CONNECTION, instead of modifying the config.
DB_CONNECTION=pgsql
I would like to add sqlite db to my lumen app but I have some troubles.
I create migration:
php artisan make:migration create_users_table --create=users
then I changed my .env file, so it looks like:
DB_CONNECTION=sqlite
DB_HOST=localhost
DB_DATABASE=database.sqlite
then I was created database.sqlite and put it in storage folder and when I 'm trying to do:
php artisan migrate
I have
[InvalidArgumentException]
Database (database.sqlite) does not exist.
I uncommented this lines in bootstrap/app.php:
Dotenv::load(__DIR__.'/../');
$app->withFacades();
I can't find what is wrong.
I work on ubuntu 14.04
In my .env file I changed to:
DB_CONNECTION=sqlite
# DB_HOST=localhost
# DB_DATABASE=database.sqlite
I left only
DB_CONNECTION=sqlite
So Lumen use default config from /vendor/laravel/lumen-framework/config/database.php. It works.
According to lumen-framwork/config/database.php
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', storage_path('database.sqlite')),
'prefix' => env('DB_PREFIX', ''),
],
sqlite is the default setting for lumen.
And you can set DB_CONNECTION in .env as follows:
DB_CONNECTION=sqlite
Then lumen will use storage_path('database.sqlite') as storage/database.sqlite
Otherwise, if you would like to assign DB_DATABASE directly, you should give the full path:
DB_DATABASE=/Users/../../storage/database.sqlite
Step 1 : Open .env file & replace respective database configuration with following piece of code.
DB_CONNECTION = sqlite
#DB_HOST = 127.0.0.1
#DB_PORT = 3306
#DB_DATABASE = homestead
#DB_USERNAME = homestead
#DB_PASSWORD = secret
Note : lines those are started with # are basically commented code.
Step 2 : Create a new file, name it database.sqlite in database folder. It will store out database structure.
Step 3 : There is need to include this file as ignored in our versioning system as there will be so many changes are going to be made with database i.e. insert, delete, update etc. To ignore database.sqlite, Open .gitignore file, and add this line at the end database/database.sqlite. (i.e. path of database.sqlite file).
I started learning Laravel just an hour ago and following tutorials.
My settings for database are as follow (File: app/config/database.php):
'default' => 'mysql',
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laraveltest',
'username' => 'root',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
In mySQL on homestead, I already created laraveltest database. I also created migration file in database/migration directory with following code:
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('name');
$table->timestamps();
});
}
and migration commands were:
vagrant#homestead:~/Code/Laravel$ php artisan migrate
Migration table created successfully.
Migrated: 2014_08_14_195103_create_users_table
As displayed, table users created but in homestead database, not in laraveltest database. Can someone please tell where I went wrong and how to force laravel to use laraveltest database?
Edit after first comment
File: bootstrap/start.php
$env = $app->detectEnvironment(array(
'local' => array('homestead'),
));
It is most likely an issue with environments. Check that you have a database configuration file in app/config/local/, and check that it has the proper settings.
If anyone finds this looking for the answer for Laravel 5 or later: It's the same as #camroncade says, but in this case it's your .env file in the project root you want to look at.
I am currently dealing with this issue as well. Changed the .env file numerous times as well as the config.php file. Nothing seemed to work.
After having done some deep digging into the framework I have found an acceptable temporary workaround to the problem that does not conflict with ANY of Laravel 5.0.2's Exceptions.
NOTE: I'm currently using Ampps 3.4 with php 7.0.2 phpmyadmin 4.4.15.1 on a Windows 8.1 OS.
Inside of the file "ConnectionFactory.php" (located at "laravel\vendor\laravel\framework\src\Illuminate\Database\Connectors"),
scroll down to the public function make located at line 41.
Within this function you can do the following after this statement $config = $this->parseConfig($config, $name); which should be on line 43:
$config['database'] = 'Insert the name of your database here';
This change will allow Laravel to continue with its normal process having no negative effect anywhere with the framework or your project. That is until this bug is fixed by the makers of Laravel or the community.
Till then happy coding! =)