tiangolo / full-stack-fastapi-template

Full stack, modern web application template. Using FastAPI, React, SQLModel, PostgreSQL, Docker, GitHub Actions, automatic HTTPS and more.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to query the database?

chhenning opened this issue · comments

For everyone who has the same issues as me.

1. Connect to the database server via pgadmin

Once the docker-compose up -d finished. pgadmin is available on localhost:5050. After logging in make sure to use the following to connect to the PostgreSQL server:

host: db
port: 5432
username: postgres
password: changethis

Please note NOT to use localhost:5432 to connect to the database server.

2. Query user table

Make sure to put the table name in quotes!

select * from "user";

If you just do select * from user; you wont see your username and that is it.

3. Connect to the database server via psql

We need to expose the db port to the host system (aka your laptop). In the docker-compose.yml do this:

services:
  db:
    ports:
      - '5432:5432'

If you don't do this you will see the following error:

psql: error: connection to server at "localhost" (::1), port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?

After the change, you can connect now via:

psql postgresql://postgres:changethis@localhost:5432/app