RifkyMuhamad / my-problem

just problem
0 stars 0 forks source link

Cara Connect Laravel ke PostgreSQL Vercel #2

Open RifkyMuhamad opened 10 months ago

RifkyMuhamad commented 10 months ago

Cara 1

.env

DB_CONNECTION=pgsql
DB_HOST=[db-host]
DB_PORT=5432
DB_DATABASE=laravel_api
DB_USERNAME=[db-username]
DB_PASSWORD=[db-password]

config/database.php

'pgsql' => [
    'driver' => 'pgsql',
    'url' => env('DATABASE_URL'),
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'laravel_api'),
    'username' => env('DB_USERNAME', 'postgres'),
    'password' => env('DB_PASSWORD', 'postgres'),
    'charset' => 'utf8',
    'prefix' => '',
    'prefix_indexes' => true,
    'search_path' => 'public',

    // by default sslmode is 'prefer'
    'sslmode' => 'require',
]

Problem

Illuminate\Database\QueryException

SQLSTATE[08006] [7] ERROR:  Endpoint ID is not specified.
Either please upgrade the postgres client library (libpq) for SNI
support or pass the endpoint ID (first part of the domain name) as a parameter: '?options=endpoint%3D<endpoint-id>'.
See more at https://neon.tech/sni
(Connection: pgsql, SQL: select * from information_schema.tables
where table_catalog = verceldb and table_schema = public and table_name = migrations and table_type = 'BASE TABLE')

  at vendor\laravel\framework\src\Illuminate\Database\Connection.php:822
    818▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    819▕                 );
    820▕             }
    821▕
  ➜ 822▕             throw new QueryException(
    823▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    824▕             );
    825▕         }
    826▕     }

  1   vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:65
      PDOException::("SQLSTATE[08006] [7] ERROR:  Endpoint ID is not specified. Either please upgrade the postgres
client library (libpq) for SNI support or pass the endpoint ID (first part of the domain name) as a parameter:
'?options=endpoint%3D<endpoint-id>'. See more at https://neon.tech/sni")

  2   vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:65
      PDO::__construct("pgsql:host=[db-host];dbname=laravel_api';port=5432;sslmode=require", "default",
Object(SensitiveParameterValue), [])

Cara 2

.env

DB_CONNECTION=pgsql
+ DATABASE_URL=postgres://[user]:[password]@[neon_hostname]/[dbname]?options=endpoint%3D[endpoint-id]
DB_HOST=[db-host]
DB_PORT=5432
DB_DATABASE=laravel_api
DB_USERNAME=[db-username]
DB_PASSWORD=[db-password]

config/database.php

'pgsql' => [
    'driver' => 'pgsql',

    // by default is call DATABASE_URL
    'url' => env('DATABASE_URL'),
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'laravel_api'),
    'username' => env('DB_USERNAME', 'postgres'),
    'password' => env('DB_PASSWORD', 'postgres'),
    'charset' => 'utf8',
    'prefix' => '',
    'prefix_indexes' => true,
    'search_path' => 'public',
    'sslmode' => 'require',
]

Problem

TypeError 

 array_diff_key(): Argument #2 must be of type array, string given

  at vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:99
     95▕     public function getOptions(array $config)
     96▕     {
     97▕         $options = $config['options'] ?? [];
     98▕
  ➜  99▕         return array_diff_key($this->options, $options) + $options;
    100▕     }
    101▕
    102▕     /**
    103▕      * Get the default PDO connection options.

  1   vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:99

  2   vendor\laravel\framework\src\Illuminate\Database\Connectors\PostgresConnector.php:36
      Illuminate\Database\Connectors\Connector::getOptions()
horgolzari98 commented 10 months ago

It looks like you are encountering two separate issues related to the PostgreSQL configuration in your Laravel setup. Let's address each problem individually:

Problem 1: SQLSTATE[08006] [7] ERROR: Endpoint ID is not specified.

This error suggests an issue with PostgreSQL and SNI (Server Name Indication) support. It provides a possible solution by either upgrading the PostgreSQL client library (libpq) for SNI support or passing the endpoint ID as a parameter.

Solution:

  1. Upgrade libpq: Ensure that you are using a version of libpq that supports SNI. You may need to upgrade your PostgreSQL client library.

  2. Update Laravel Configuration: In your .env file, update the DB_CONNECTION and DB_HOST values as follows:

    DB_CONNECTION=pgsql
    DB_HOST=[neon_hostname]

    Additionally, you can try setting the DATABASE_URL in the .env file:

    DATABASE_URL=postgres://[user]:[password]@[neon_hostname]/[dbname]?options=endpoint%3D[endpoint-id]

    Make sure to replace [user], [password], [neon_hostname], [dbname], and [endpoint-id] with your actual PostgreSQL credentials and information.

Problem 2: TypeError - array_diff_key()

This error is related to a TypeError in the array_diff_key() function in the Connector.php file.

Solution:

It appears that there might be an issue with the options array. Make sure that the 'options' key in your PostgreSQL configuration is an array.

Update your PostgreSQL configuration in config/database.php to ensure the 'options' key is an array:

'pgsql' => [
    // ... other configurations
    'options' => [], // Add this line
],

This should resolve the TypeError related to array_diff_key().

After making these changes, try running your Laravel application again and check if the issues are resolved. If you encounter any further problems or if the error messages change, please provide additional details for further assistance.