casdoor / casdoor

An open-source UI-first Identity and Access Management (IAM) / Single-Sign-On (SSO) platform with web UI supporting OAuth 2.0, OIDC, SAML, CAS, LDAP, SCIM, WebAuthn, TOTP, MFA, Face ID, RADIUS, Google Workspace, Active Directory and Kerberos
https://casdoor.org
Apache License 2.0
9.2k stars 1.09k forks source link

PostgreSQL database name error #2955

Closed emperorkebab closed 1 month ago

emperorkebab commented 1 month ago

Postgres doesn't work with the current version of casdoor

Here is my docker compose file

version: '3.1'

services:
  myiam:
    image: casbin/casdoor
    container_name: myiam
    entrypoint: /bin/sh -c './server --createDatabase=true'
    networks:
      - mynetwork
    environment:
      RUNNING_IN_DOCKER: "true"
      appname: myiam
      httpport: 8000
      runmode: dev
      copyrequestbody: true

      driverName: postgres
      # Setting 1
      dataSourceName: "user=db_user password=db_password host=mydb_service port=5432 sslmode=disable dbname=myiam"
      # Setting 2
      # dataSourceName: "db_user:db_password@mydb_service:5432/myiam?sslmode=disable"
      dbName: myiam
      showSql: false
      tableNamePrefix: 
      redisEndpoint: 

      defaultStorageProvider: 
      isCloudIntranet: false
      authState: "myiam"
      socks5Proxy: "127.0.0.1:10808"
      verificationCodeTimeout: 10
      initScore: 0
      logPostOnly: true

      origin: 

      isDemoMode: false
      batchSize: 100
      enableGzip: true

      quota: '{"organization": -1, "user": -1, "application": -1, "provider": -1}'
      logConfig: '{"filename": "logs/myiam.log", "maxdays":99999, "perm":"0770"}'
      initDataFile: "./init_data.json"
      frontendBaseDir: "../myiam"
    ports:
      - "80:8000"
    depends_on:
      - mydb_service

  mydb_service:
    image: postgres:14
    container_name: mydb_service
    environment:
      POSTGRES_PASSWORD: db_password
      POSTGRES_DB: myiam
      POSTGRES_USER: db_user
    ports:
      - "5432:5432"
    networks:
      - mynetwork
    volumes:
      - home/user/docker/myiam_service:/var/lib/postgresql/data

networks:
  mynetwork:

Log using setting 1:

myiam container:

panic: dial tcp 172.22.0.2:5432: connect: connection refused
goroutine 1 [running]:
github.com/casdoor/casdoor/object.InitAdapter()
  /go/src/casdoor/object/ormer.go:91 +0x246
main.main()
  /go/src/casdoor/main.go:35 +0x30
panic: pq: database "db_user" does not exist
goroutine 1 [running]:
github.com/casdoor/casdoor/object.InitAdapter()
  /go/src/casdoor/object/ormer.go:91 +0x246
main.main()
  /go/src/casdoor/main.go:35 +0x30

mydb container:

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
Success. You can now start the database server using:
    pg_ctl -D /var/lib/postgresql/data -l logfile start
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
waiting for server to start....2024-05-17 15:58:40.808 UTC [49] LOG:  starting PostgreSQL 14.12 (Debian 14.12-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
2024-05-17 15:58:40.810 UTC [49] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-05-17 15:58:40.817 UTC [50] LOG:  database system was shut down at 2024-05-17 15:58:40 UTC
2024-05-17 15:58:40.822 UTC [49] LOG:  database system is ready to accept connections
 done
server started
CREATE DATABASE
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
2024-05-17 15:58:41.117 UTC [49] LOG:  received fast shutdown request
waiting for server to shut down....2024-05-17 15:58:41.118 UTC [49] LOG:  aborting any active transactions
2024-05-17 15:58:41.120 UTC [49] LOG:  background worker "logical replication launcher" (PID 56) exited with exit code 1
2024-05-17 15:58:41.120 UTC [51] LOG:  shutting down
2024-05-17 15:58:41.133 UTC [49] LOG:  database system is shut down
 done
server stopped
PostgreSQL init process complete; ready for start up.
2024-05-17 15:58:41.245 UTC [1] LOG:  starting PostgreSQL 14.12 (Debian 14.12-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
2024-05-17 15:58:41.245 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2024-05-17 15:58:41.245 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2024-05-17 15:58:41.248 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-05-17 15:58:41.253 UTC [64] LOG:  database system was shut down at 2024-05-17 15:58:41 UTC
2024-05-17 15:58:41.258 UTC [1] LOG:  database system is ready to accept connections
2024-05-17 15:58:41.506 UTC [71] FATAL:  database "db_user" does not exist
2024-05-17 15:58:42.547 UTC [72] FATAL:  database "db_user" does not exist
2024-05-17 15:58:44.398 UTC [73] FATAL:  database "db_user" does not exist
2024-05-17 15:58:47.835 UTC [74] FATAL:  database "db_user" does not exist
2024-05-17 15:58:54.463 UTC [75] FATAL:  database "db_user" does not exist
2024-05-17 15:59:07.508 UTC [76] FATAL:  database "db_user" does not exist
2024-05-17 15:59:33.356 UTC [77] FATAL:  database "db_user" does not exist

Log using setting 2:

myiam container:

panic: missing "=" after "db_user:db_password@mydb_service:5432/myiam" in connection info string"
goroutine 1 [running]:
github.com/casdoor/casdoor/object.InitAdapter()
  /go/src/casdoor/object/ormer.go:91 +0x246
main.main()
  /go/src/casdoor/main.go:35 +0x30

mydb container:

The same as using setting 1 (without the last FATAL: database "db_user" does not exist lines)


The error for setting2 is expected because casdoor doesn't use postgres connection uri. But setting1 error is unexpected because it uses the postgres connection string in accordance with the casdoor documentation

It would be nice to support postgres connection uri, or fix the current connection method please

The log points to this line: https://github.com/casdoor/casdoor/blob/2daf26aa884dfe3f27efb25382d111bc1efa3af5/object/ormer.go#L96 I can successfully log into the database with pgadmin using db_user & db_password (the "myiam" db is there but empty) so the user is created, and it seems that the error is in casdoor being the database username is used as the database name?

casbin-bot commented 1 month ago

@tangyang9464 @JalinWang @imp2002

hsluoyz commented 1 month ago

@emperorkebab I'm not going to debug for your Dockerfile. You need to look carefully at the official docs: https://casdoor.org/docs/basic/try-with-docker/#get-the-image . Follow the instructions in it.

emperorkebab commented 1 month ago

Hi i the problem is somewhere in the ORM implementation, i think it's the same whether a docker compose file is used or not. I have also seen other users online say they struggle to make casdoor work with postgres, so i don't think i'm alone to have this bug

hsluoyz commented 1 month ago

@emperorkebab you created that db by your code, that db doesn't work. It's your own responsibility. Ask in Stackoverflow as a programming question, not here. This place is more for Casdoor bug report, not fixing your own issue.