Forums

error trying to convert sqlite db to postgresql

I created a new web app to use a postgresql db but didn't realize the settings.py DATABASES section was configured like this:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }

I have a field in one of the models that needs to be a JSONField.

I have already created the model.py but no data has been stored in the db yet.

I followed an online post which suggested I just needed to change my DATABASE setting to this:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'NAME_OF_DB', 'USER': 'DB_USER_NAME', 'PASSWORD': 'DB_PASSWORD', 'HOST': 'localhost', 'PORT': 'PORT_NUMBER', } }

Obviously this caused all sorts of problems. I removed all but the ENGINE and NAME entry and got this error when running 'python manage.py makemigrations'.

I've reset to sqlite3 for testing but at some point would like to change to postgresql.

How do I go about that?

Traceback (most recent call last): File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/db/backends/base/base. py", line 217, in ensure_connection self.connect() File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/db/backends/base/base. py", line 195, in connect self.connection = self.get_new_connection(conn_params) File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/db/backends/postgresql /base.py", line 178, in get_new_connection connection = Database.connect(conn_params) File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/psycopg2/init.py", line 1 27, in connect conn = _connect(dsn, connection_factory=connection_factory, kwasync) psycopg2.OperationalError: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? The above exception was the direct cause of the following exception: output = self.handle(args, *options)

Traceback (most recent call last): File "manage.py", line 21, in <module> main() File "manage.py", line 17, in main execute_from_command_line(sys.argv) File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/core/management/init.py", line 381, in execute_from_command_line utility.execute() File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/core/management/init.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/core/management/base.py", line 323, in run_from_argv self.execute(args, cmd_options) File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/core/management/base.py", line 364, in execute output = self.handle(args, options) File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped res = handle_func(*args, kwargs) File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/core/management/commands/makemigrations.py", line 101, in handle loader.check_consistent_history(connection) File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/db/migrations/loader.py", line 283, in check_consistent_history applied = recorder.applied_migrations() File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 73, in applied_migrations if self.has_table(): File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 56, in has_table return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()) File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/db/backends/base/base.py", line 256, in cursor return self._cursor() File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/db/backends/base/base.py", line 233, in _cursor self.ensure_connection() File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection self.connect() File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/db/utils.py", line 89, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection self.connect() File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/db/backends/base/base.py", line 195, in connect self.connection = self.get_new_connection(conn_params) File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection connection = Database.connect(conn_params) File "/home/noahmizrahigg1/.virtualenvs/gg1/lib/python3.8/site-packages/psycopg2/init.py", line 127, in connect conn = _connect(dsn, connection_factory=connection_factory, kwasync) django.db.utils.OperationalError: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

The details that you need to connect to your postgres database are on the Postgres tab of your Databases page.