前提・実現したいこと
git push heroku mainで"Verifying deploy... done."と表示されているため、
pushまでは正常に出来ていると思います。
その後、"hroku run python manage.py migrate"すると、django.db.utils.OperationalErrorとなりmigrateが完了できません。
エラーの中身を見るとpsycopg2.OperationalErrorもありました。
何かherokuへのpushからmigrateまでの間に何か抜けているのでしょうか?
settings.pyがおかしいのでしょうか?
googleで検索しましたが解消方法が分かりませんでしたので、何方かご教示お願い致します。
↓参考サイト
https://qiita.com/hagyyyy1992/items/466b5bab67118175be65
発生している問題・エラーメッセージ
hroku run python manage.py migrateの実行結果
error
1Traceback (most recent call last): 2 File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 3216, in ensure_connection 4 self.connect() 5 File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 6194, in connect 7 self.connection = self.get_new_connection(conn_params) 8 File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", 9 line 168, in get_new_connection 10 connection = Database.connect(**conn_params) 11 File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 127, in con 12nect 13 conn = _connect(dsn, connection_factory=connection_factory, **kwasync) 14psycopg2.OperationalError: could not connect to server: No such file or directory 15 Is the server running locally and accepting 16 connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? 17 18 19The above exception was the direct cause of the following exception: 20 21Traceback (most recent call last): 22 File "manage.py", line 21, in <module> 23 main() 24 File "manage.py", line 17, in main 25 execute_from_command_line(sys.argv) 26 File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", li 27ne 371, in execute_from_command_line 28 utility.execute() 29 File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", li 30ne 365, in execute 31 self.fetch_command(subcommand).run_from_argv(self.argv) 32 File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 2 3388, in run_from_argv 34 self.execute(*args, **cmd_options) 35 File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 3 3635, in execute 37 output = self.handle(*args, **options) 38 File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate 39.py", line 77, in handle 40 connection.prepare_database() 41 File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/db/backends/postgis/ 42base.py", line 25, in prepare_database 43 with self.cursor() as cursor: 44 File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 45255, in cursor 46 return self._cursor() 47 File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 48232, in _cursor 49 self.ensure_connection() 50 File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 51216, in ensure_connection 52 self.connect() 53 File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit 54__ 55 raise dj_exc_value.with_traceback(traceback) from exc_value 56 File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 57216, in ensure_connection 58 self.connect() 59 File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 60194, in connect 61 self.connection = self.get_new_connection(conn_params) 62 File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", 63 line 168, in get_new_connection 64 connection = Database.connect(**conn_params) 65 File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 127, in con 66nect 67 conn = _connect(dsn, connection_factory=connection_factory, **kwasync) 68django.db.utils.OperationalError: could not connect to server: No such file or directory 69 Is the server running locally and accepting 70 connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? 71
該当のソースコード
settings
1import os 2from django.contrib import admin 3 4###admin画面の表示設定 5admin.AdminSite.site_title = 'ログイン' 6admin.AdminSite.site_header = '管 理 画 面' 7admin.AdminSite.index_title = 'メニュー' 8###ログイン関連 9LOGIN_URL='/accounts/login'#<-- ログインURL 10LOGIN_REDIRECT_URL='/'#<-- ログイン後トップページにリダイレクト 11LOGOUT_REDIRECT_URL='/'#<-- ログアウト後トップページにリダイレクト 12 13#####GDALに関するエラー回避 windowsの場合必要 14if os.name == 'nt': 15 import platform 16 17 POSTGRES = r"C:\Program Files\PostgreSQL\9.6" 18 OSGEO4W = r"C:\OSGeo4W" 19 if '64' in platform.architecture()[0]: 20 OSGEO4W += "64" 21 assert os.path.isdir(OSGEO4W), "Directory does not exist: " + OSGEO4W 22 23 os.environ['OSGEO4W_ROOT'] = OSGEO4W 24 os.environ['POSTGRES_ROOT'] = POSTGRES 25 os.environ['GDAL_LIBRARY_PATH'] = OSGEO4W + r"\bin" 26 os.environ['GEOS_LIBRARY_PATH'] = OSGEO4W + r"\bin" 27 os.environ['GDAL_DATA'] = OSGEO4W + r"\share\gdal" 28 os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj" 29 os.environ['PATH'] = OSGEO4W + r"\bin;" + POSTGRES + r"\bin;" + os.environ['PATH'] 30##### 31 32# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 33BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 34 35# Quick-start development settings - unsuitable for production 36# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ 37 38# SECURITY WARNING: keep the secret key used in production secret! 39SECRET_KEY = '118#9w_tjpu^(3kq1#2f356&%t-h6&^o$ugxlqt^br^2ei_3j(' 40 41# SECURITY WARNING: don't run with debug turned on in production! 42DEBUG = False #True 43 44ALLOWED_HOSTS = [ 45 "*" 46] 47 48 49MIDDLEWARE = [ 50 'django.middleware.security.SecurityMiddleware', 51 'django.contrib.sessions.middleware.SessionMiddleware', 52 'django.middleware.common.CommonMiddleware', 53 'django.middleware.csrf.CsrfViewMiddleware', 54 'django.contrib.auth.middleware.AuthenticationMiddleware', 55 'django.contrib.messages.middleware.MessageMiddleware', 56 'django.middleware.clickjacking.XFrameOptionsMiddleware', 57 'whitenoise.middleware.WhiteNoiseMiddleware',#heroku追加 58] 59 60ROOT_URLCONF = 'geodj.urls' 61 62TEMPLATES = [ 63 { 64 'BACKEND': 'django.template.backends.django.DjangoTemplates', 65 'DIRS': [os.path.join(BASE_DIR, 'templates')], 66 'APP_DIRS': True, 67 'OPTIONS': { 68 'context_processors': [ 69 'django.template.context_processors.debug', 70 'django.template.context_processors.request', 71 'django.contrib.auth.context_processors.auth', 72 'django.contrib.messages.context_processors.messages', 73 ], 74 }, 75 }, 76] 77 78WSGI_APPLICATION = 'geodj.wsgi.application' 79 80 81# Database 82# https://docs.djangoproject.com/en/3.0/ref/settings/#databases 83 84DATABASES = { 85 'default': { 86 'ENGINE': 'django.contrib.gis.db.backends.postgis', 87 'NAME': 'geodjangodb2', 88 'USER': 'geo_admin', 89 'PASSWORD': 'geoadmin', 90 'HOST': 'localhost', 91 'PORT':'5432',#heroku 92 }, 93} 94 95INSTALLED_APPS = [ 96 'django.contrib.admin', 97 'django.contrib.auth', 98 'django.contrib.contenttypes', 99 'django.contrib.sessions', 100 'django.contrib.messages', 101 'django.contrib.staticfiles', 102 'django.templatetags.static', 103 'django.contrib.gis', 104 'django_cleanup.apps.CleanupConfig', # django-cleanup 105 'map.apps.MapConfig', 106 'geodj', 107 'leaflet', 108 'six', 109 'django_filters', 110 'rest_framework', 111 'rest_framework_gis', 112 'markdown', 113 'PIL', 114 'PIL.ExifTags', 115] 116 117# Password validation 118# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators 119 120AUTH_PASSWORD_VALIDATORS = [ 121 { 122 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 123 }, 124 { 125 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 126 }, 127 { 128 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 129 }, 130 { 131 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 132 }, 133] 134 135 136# Internationalization 137# https://docs.djangoproject.com/en/3.0/topics/i18n/ 138 139LANGUAGE_CODE = 'ja-JP' 140 141TIME_ZONE = 'Asia/Tokyo' 142 143USE_I18N = True 144 145USE_L10N = True 146 147USE_TZ = True 148 149# ログイン後、10時間でタイムアウト 150SESSION_COOKIE_AGE = 36000 151 152# Static files (CSS, JavaScript, Images) 153# https://docs.djangoproject.com/en/3.0/howto/static-files/ 154 155STATIC_URL = '/static/' 156STATIC_ROOT = os.path.join(BASE_DIR, 'static') 157STATICFILES_DIRS = (os.path.join(BASE_DIR, 'map/static'),) 158# ↓ heroku追加 159STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' 160 161MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 162MEDIA_URL = '/media/' 163 164# heroku用 165try: 166 from .local_settings import * 167except ImportError: 168 pass 169 170if not DEBUG: 171 import django_heroku 172 django_heroku.settings(locals()) 173 import dj_database_url 174 DATABASES['default'] = dj_database_url.config() 175 DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis' 176 GDAL_LIBRARY_PATH = os.getenv('GDAL_LIBRARY_PATH') 177 GEOS_LIBRARY_PATH = os.getenv('GEOS_LIBRARY_PATH') 178# ~heroku用
試したこと
settings.pyのDATABASEのPORTにブランクから5432に修正。
postgisをインストールしていなかったので、create extension postgis実施。
上記をしても同じエラーがでる。
ローカルでpython manage.py runserverしたところ、同じエラーがでました。
settings.pyの最後にheroku用として追加したtry: ~ except ImportError:passを
コメントアウトするとrunserverできます。
ということは、本番とローカルでsettings.pyを分けた際の分け方、
書き方がおかしいということ?
補足情報(FW/ツールのバージョンなど)
windows10
django 2.0.8
python 3.6.10
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。