Serverpod sql error when starting a clean project

1.1k views Asked by At

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.

3

There are 3 answers

0
Isak dl On BEST ANSWER

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.

0
Madhan On

Step 1: List out images in docker

// for Linux [if you got permission error use sudo]
    $docker ps  
    

enter image description here Step 2 : Execute following command and replace Container Name with Container ID

// In container name You need to replace with container ID

  $ docker cp ./generated/tables-serverpod.pgsql <container_name>:/docker-entrypoint-initdb.d/tables-serverpod.pgsql

  

Step 3: Replace container id and db name and execute following code

  // here you need to replace with postgres image container id
  $ docker exec -u postgres <container_name> psql <db_name> postgres -f docker-entrypoint-initdb.d/tables-serverpod.pgsql

step 4: Then Run

$dart run bin/main.dart
2
Husain N On

I have done the same creating flutter serverpod project. when I run my project on other Mac(not the host one). it used to give me error like you mentioned above. but then I generate the basic serverpod settings as suggested by above answer. the problem now is I had also created the USER table in the host Mac with 3-4 records of users. now I don't see the user table at all when using the project on other Mac. so basically how do I run the whole flutter serverpod project with records and everything remotely via different mac's.