1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Laravel】Vercel環境でのPostgreSQL接続

Last updated at Posted at 2025-01-03

手順

  1. Nodeのバージョンを18系に指定
  2. vercel.jsonを設定
  3. config/database.phpの設定

Let's implement it!!

1. Nodeのバージョンを18系に指定

VercelのConsoleから設定を行います。

スクリーンショット 2024-12-18 15.22.53.png

package.json内にて、Nodeのバージョンを指定します。

package.json
{
    "private": true,
    "type": "module",
    "scripts": {
        "build": "vite build",
        "dev": "vite"
    },
    "devDependencies": {
        "autoprefixer": "^10.4.20",
        "axios": "^1.7.4",
        "concurrently": "^9.0.1",
        "laravel-vite-plugin": "^1.0",
        "postcss": "^8.4.47",
        "tailwindcss": "^3.4.13",
        "vite": "^5.0"
    },
    "engines": {
        "node": "18.x", // ここ
        "npm": "10.x" // ここ
    },
    "volta": {
        "node": "18.20.5",
        "npm": "10.9.2"
    }
}

普通はこれでいいと思いますが...。
僕の環境では「20系」からの変更だったので、各ライブラリが動作しなくなりました。

以下を実行し、18系にあったパッケージをインストールし直しましょう。

rm -rf node_modules package-lock.json

2. vercel.jsonを設定

vercel.json
{
    "version": 2,
    "framework": null,
    "builds": [
        {
            "src": "public/**/*",
            "use": "@vercel/static"
        },
        {
            "src": "api/*.php",
            "use": "vercel-php@0.7.3"
        }
    ],
    "routes": [
        {
            "src": "^/(css|js|images|fonts|storage)/(.*)",
            "dest": "/public/$1/$2"
        },
        {
            "src": "^/favicon.ico",
            "dest": "/public/favicon.ico"
        },
        {
            "src": "/(.*)",
            "dest": "/api/index.php"
        }
    ],
    "env": {
        "APP_NAME": "アプリ名",
        "APP_ENV": "production",
        "APP_URL": "ドメイン",
        "VERCEL_DEMO_MODE": "true",
        "APP_CONFIG_CACHE": "/tmp/config.php",
        "APP_EVENTS_CACHE": "/tmp/events.php",
        "APP_PACKAGES_CACHE": "/tmp/packages.php",
        "APP_ROUTES_CACHE": "/tmp/routes.php",
        "APP_SERVICES_CACHE": "/tmp/services.php",
        "CACHE_DRIVER": "array",
        "LOG_CHANNEL": "stderr",
        "VIEW_COMPILED_PATH": "/tmp/views",
        "SSR_TEMP_PATH": "/tmp/ssr",
        "NODE_PATH": "node"
    }
}

3. config/database.phpの設定

'pgsql' => [
    'driver' => 'pgsql',
    'url' => env('POSTGRES_URL_NON_POOLING'),
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'prefix_indexes' => true,
    'sslmode' => 'require',
],
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?