flutterdartbackendserverpod

Serverpod sql error when starting a clean project


I have created a new serverpod project and launched the docker containers for the database and redis. But when I launch the server it self I get the following error:

Failed to connect to the database. Retrying in 10 seconds. PostgreSQLSeverity.error 42P01: relation "serverpod_runtime_settings" does not exist

I have tried following the get started steps here: https://docs.serverpod.dev/ but still running into this issue.


Solution

  • Edit: As of version 1.2 (2024-01-15) Serverpod has built in migration support.

    In a new clean project all you have to do is add the flag --apply-migrations when starting the server.

    $ dart bin/main.dart --apply-migrations
    

    To create a new migration when changing the models you can run this command:

    $ serverpod create-migration
    

    After creating a new migration you will have to apply the changes when starting the server.

    Pre 1.2 solution

    The documentation is not clear on this, but serverpod comes with a set of database tables that needs to be initialized before you can run the server.

    From your server project run:

    $ docker cp ./generated/tables-serverpod.pgsql <container_name>:/docker-entrypoint-initdb.d/tables-serverpod.pgsql
    
    $ docker exec -u postgres <container_name> psql <db_name> postgres -f docker-entrypoint-initdb.d/tables-serverpod.pgsql
    

    <container_name> is normally your_project_server-postgres but you can find the exact name by running $ docker ps assuming the container is running. If not start it first with $ docker-compose up --build --detach.

    <db_name> is normally the same as project_name but for the development server the name is defined inside this file: your_project/your_project_server/config/development.yaml


    What is going on here? You need create the database tables and the code for this comes with serverpod already and all you need to do is run it on your database!

    You can find the sql code in this path: your_project/your_project_server/generated/tables-serverpod.pgsql

    The command above will first copy the pgsql file into the docker container that is running and then with the second command you are executing the sql code on the database! Meaning you will create all the tables that are needed. As an FYI you can use this same method when setting up any serverpod modules such as severpod_auth as they also come with a set of database tables see documentation on this here: https://docs.serverpod.dev/concepts/modules.


    It is also possible to use a gui tool such as https://www.pgadmin.org/ or https://eggerapps.at/postico2/. To then connect to the db you can find the username and password inside these files: your_project/your_project_server/config/development.yaml and your_project/your_project_server/config/passwords.yaml using localhost:8090 as the address (this is the default). Then copy paste the code from the pgsql file into a window where you can execute sql code and then execute it.