anschelburk / math_homework

Apache License 2.0
0 stars 0 forks source link

Install Postgres and get it working with Django #26

Closed bbelderbos closed 6 months ago

bbelderbos commented 7 months ago

I made this custom tutorial for you, hope this works: https://chat.openai.com/share/356f2ace-16e3-4dfb-a3a8-36d3b3318984

anschelburk commented 7 months ago

Thank you. Working on this now.

anschelburk commented 7 months ago

When I verified the installation (Step 1, final bullet point), I got an error in SQL Shell:

image

I asked Microsoft Copilot about it, and got this response with some suggested steps. Thinking about our recent conversation (about CRLF/LF), I'm a little unsure about whether this step is necessary. What do you think?

Appreciate it. Looking forward to continuing this tutorial!

anschelburk commented 7 months ago

Working on Step 5 of the tutorial. When I tried to run the following command,

python manage.py migrate

I got several errors in a row. For context, here's the GPT conversation I had around what I saw, and what GPT suggested:

The steps GPT is suggesting don't seem to match what I need to do. Will keep troubleshooting, but if you have any thoughts, please let me know.

bbelderbos commented 7 months ago

"Remember that this warning is related to character encoding and won’t affect the functionality of PostgreSQL itself." - as long as the prompt works, in the sense that you can type commands, let's ignore this. Btw I forgot to mention but I recommend this additional tool (you'd need to install separately) for a better/nicer postgres shell: https://www.pgcli.com

bbelderbos commented 7 months ago

Regarding the first thread, I recommend using dj-database-url as per the gpt tutorial I put together: https://chat.openai.com/share/356f2ace-16e3-4dfb-a3a8-36d3b3318984

import dj_database_url

DATABASES = {
    'default': dj_database_url.config(
        default=config('DATABASE_URL')
    )
}

Then you can set the database as just one config variable = easier.

bbelderbos commented 7 months ago

Sorry you did, I should have reviewed the other threads first. Can you paste me the complete python traceback you're getting here? Also can you verify that postgres is running and that you can connect to the DB you created manually from the command line?

bbelderbos commented 7 months ago

Did you make more progress? Easier to look together in our meeting tomorrow, but could still check anything in the next hour or so ...

anschelburk commented 7 months ago

Hi Bob! I spent the morning working on some other tasks, and am going to work on this now, through the afternoon. I took a look at your comments, and will respond to each one momentarily.

anschelburk commented 7 months ago

Sorry you did, I should have reviewed the other threads first. Can you paste me the complete python traceback you're getting here?

Yes. I actually tried several different steps with GPT and tried to link them yesterday in a comment, but for some reason it isn't showing up here. I pushed my code to GitHub before I signed off yesterday, but can also send the complete traceback - please see below. (This is copied from Django's server, rather than VS Code, if it impacts anything.)

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/admin/mathwork/mathassignment/

Django Version: 5.0.4
Python Version: 3.12.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'mathwork.apps.MathworkConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback (most recent call last):
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\contrib\sessions\backends\base.py", line 187, in _get_session
    return self._session_cache
           ^^^^^^^^^^^^^^^^^^^

During handling of the above exception ('SessionStore' object has no attribute '_session_cache'), another exception occurred:
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\contrib\admin\options.py", line 716, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\utils\decorators.py", line 188, in _view_wrapper
    result = _process_exception(request, e)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\utils\decorators.py", line 186, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\views\decorators\cache.py", line 80, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\contrib\admin\sites.py", line 228, in inner
    if not self.has_permission(request):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\contrib\admin\sites.py", line 201, in has_permission
    return request.user.is_active and request.user.is_staff
           ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\utils\functional.py", line 251, in inner
    self._setup()
    ^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\utils\functional.py", line 404, in _setup
    self._wrapped = self._setupfunc()
                    ^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\contrib\auth\middleware.py", line 33, in <lambda>
    request.user = SimpleLazyObject(lambda: get_user(request))
                                            ^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\contrib\auth\middleware.py", line 13, in get_user
    request._cached_user = auth.get_user(request)
                           ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\contrib\auth\__init__.py", line 209, in get_user
    user_id = _get_user_session_key(request)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\contrib\auth\__init__.py", line 62, in _get_user_session_key
    return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY])
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\contrib\sessions\backends\base.py", line 53, in __getitem__
    return self._session[key]
           ^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\contrib\sessions\backends\base.py", line 192, in _get_session
    self._session_cache = self.load()
                          ^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\contrib\sessions\backends\db.py", line 42, in load
    s = self._get_session_from_db()
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\contrib\sessions\backends\db.py", line 32, in _get_session_from_db
    return self.model.objects.get(

  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\db\models\manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\db\models\query.py", line 645, in get
    num = len(clone)
          ^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\db\models\query.py", line 382, in __len__
    self._fetch_all()
    ^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\db\models\query.py", line 1928, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\db\models\query.py", line 91, in __iter__
    results = compiler.execute_sql(

  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\db\models\sql\compiler.py", line 1560, in execute_sql
    cursor = self.connection.cursor()
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\db\backends\base\base.py", line 316, in cursor
    return self._cursor()
           ^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\db\backends\base\base.py", line 292, in _cursor
    self.ensure_connection()
    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\db\backends\base\base.py", line 275, in ensure_connection
    self.connect()
    ^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\db\backends\base\base.py", line 255, in connect
    conn_params = self.get_connection_params()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\OneDrive\Documents\Pybites\Math-Homework\venv\Lib\site-packages\django\db\backends\postgresql\base.py", line 196, in get_connection_params
    raise ImproperlyConfigured(
    ^

Exception Type: ImproperlyConfigured at /admin/mathwork/mathassignment/
Exception Value: settings.DATABASES is improperly configured. Please supply the NAME or OPTIONS['service'] value.
anschelburk commented 7 months ago

Also can you verify that postgres is running and that you can connect to the DB you created manually from the command line?

Good call. I tried to verify this using these instructions from ChatGPT, and came up with an error (screenshot below). Any idea what may be happening here?

image

anschelburk commented 7 months ago

Btw I forgot to mention but I recommend this additional tool (you'd need to install separately) for a better/nicer postgres shell: https://www.pgcli.com

Thank you - appreciate it. I added it to my venv for math-homework (the new requirements.txt file is pushed to GitHub), and pip installed it globally on my machine using Windows PowerShell, but it seems to want me to install other dependencies (see traceback below). Before I go further, I saw that pgcli seems to be primary designed for Mac and Linux, and just want to make sure that installing these dependencies globally won't cause weird behavior on my machine. Do you think it's okay to install these things, so that I can launch pgcli directly from the command line on my computer?

(I'm assuming that it's a shell you can launch, like pqsl or Git Bash. If that's a wrong assumption though, please let me know!)

PS C:\Users\ansch> pgcli
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\ansch\AppData\Local\Programs\Python\Python312\Scripts\pgcli.exe\__main__.py", line 4, in <module>
  File "C:\Users\ansch\AppData\Local\Programs\Python\Python312\Lib\site-packages\pgcli\main.py", line 2, in <module>
    from pgspecial.namedqueries import NamedQueries
  File "C:\Users\ansch\AppData\Local\Programs\Python\Python312\Lib\site-packages\pgspecial\__init__.py", line 12, in <module>
    from . import dbcommands
  File "C:\Users\ansch\AppData\Local\Programs\Python\Python312\Lib\site-packages\pgspecial\dbcommands.py", line 7, in <module>
    from psycopg.sql import SQL
  File "C:\Users\ansch\AppData\Local\Programs\Python\Python312\Lib\site-packages\psycopg\__init__.py", line 9, in <module>
    from . import pq  # noqa: F401 import early to stabilize side effects
    ^^^^^^^^^^^^^^^^
  File "C:\Users\ansch\AppData\Local\Programs\Python\Python312\Lib\site-packages\psycopg\pq\__init__.py", line 114, in <module>
    import_from_libpq()
  File "C:\Users\ansch\AppData\Local\Programs\Python\Python312\Lib\site-packages\psycopg\pq\__init__.py", line 106, in import_from_libpq
    raise ImportError(
ImportError: no pq wrapper available.
Attempts made:
- couldn't import psycopg 'c' implementation: No module named 'psycopg_c'
- couldn't import psycopg 'binary' implementation: No module named 'psycopg_binary'
- couldn't import psycopg 'python' implementation: libpq library not found
bbelderbos commented 7 months ago

I have not personally tested this on Windows, but all the underlying libraries used by this project are cross-platform compatible including Windows. If you have a Windows machine, I'd very much appreciate if you could test it out and let me know. I do want to support Windows, I just don't have the resources right now.

Sorry I missed this, let's not go that route for now and just use the more primitive psql prompt, which you should not have to use often because when it's all wired up well with Django, you'll just be writing Python to interface with the database most of the time.

bbelderbos commented 7 months ago

Here you just want to default to localhost, so just press enter there, like you seem to have done with the other options

image

What happens if you do that?

bbelderbos commented 7 months ago

Apart from that, I checked locally and also got a django / python error, turns out the setting had an extra level of nesting I fixed here: https://github.com/anschelburk/Math-Homework/pull/27/files < if you merge that new branch of mine into yours and pull the fix down to your machine, I hope it then works.

anschelburk commented 7 months ago

Noting here - for more on setting up Postgres on Mac, see Issue #29.

bbelderbos commented 6 months ago

works, and now we're using the same app as both on mac :)