tinode / chat

Instant messaging platform. Backend in Go. Clients: Swift iOS, Java Android, JS webapp, scriptable command line; chatbots
GNU General Public License v3.0
11.93k stars 1.87k forks source link

Can't initialize postgres database (does not use configured database to init) #889

Closed kfatehi closed 11 months ago

kfatehi commented 11 months ago

The init-db command does not use the database that is defined for it in the config. It attempts to use the postgres database instead of the one defined.

Repro

postgres=# create database tinode;
CREATE DATABASE
postgres=# create user tinode with encrypted password 'rj4h22h9k';
CREATE ROLE
postgres=# grant all privileges on database tinode to tinode;
GRANT
postgres=#

Config:

        "use_adapter": "postgres",

        // Configurations of individual adapters.
        "adapters": {
            // PostgreSQL configuration. See https://godoc.org/github.com/jackc/pgx#Config
            // for other possible options.
            "postgres": {
                // PostgreSQL connection settings.
                "User": "tinode",
                "Passwd": "rj4h22h9k",
                "Host": "localhost",
                "Port": "5432",
                "DBName": "tinode",

                // DSN: alternative way of specifying database configuration, passed unchanged
                // to the driver. See https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
                // "dsn": "postgresql://tinode:rj4h22h9k@localhost:5432/tinode?sslmode=disable&connect_timeout=10",

                // PostgreSQL connection pool settings.
                // Maximum number of open connections to the database. Zero means unlimited.
                "max_open_conns": 5,
                // Maximum number of connections in the idle connection pool. Zero means no idle connections are retained.
                "max_idle_conns": 5,
                // Maximum amount of time a connection may be reused. Zero means unlimited.
                "conn_max_lifetime": 60,
                // Maximum amount of time waiting for a connection from the pool. Zero means no timeout.
                "sql_timeout": 10
            },
tinode-v0.22.10$ ./init-db -data=data.json
2023/10/08 11:44:18 Database adapter: 'postgres'; version: 113
2023/10/08 11:44:18 Database not found. Creating.
2023/10/08 11:44:18 Failure: ERROR: permission denied to create database (SQLSTATE 42501)

Postgres Log:

2023-10-08 11:44:37.401 PDT [850075] [unknown]@[unknown] LOG:  connection received: host=127.0.0.1 port=42870
2023-10-08 11:44:37.402 PDT [850075] tinode@tinode LOG:  connection authorized: user=tinode database=tinode
2023-10-08 11:44:37.404 PDT [850075] tinode@tinode ERROR:  relation "kvmeta" does not exist at character 19
2023-10-08 11:44:37.404 PDT [850075] tinode@tinode STATEMENT:  SELECT value FROM kvmeta WHERE key = $1
2023-10-08 11:44:37.405 PDT [850075] tinode@tinode ERROR:  relation "kvmeta" does not exist at character 19
2023-10-08 11:44:37.405 PDT [850075] tinode@tinode STATEMENT:  SELECT value FROM kvmeta WHERE key = $1
2023-10-08 11:44:37.405 PDT [850075] tinode@tinode LOG:  disconnection: session time: 0:00:00.004 user=tinode database=tinode host=127.0.0.1 port=42870
2023-10-08 11:44:37.407 PDT [850076] [unknown]@[unknown] LOG:  connection received: host=127.0.0.1 port=42872
2023-10-08 11:44:37.407 PDT [850077] [unknown]@[unknown] LOG:  connection received: host=127.0.0.1 port=42874
2023-10-08 11:44:37.408 PDT [850076] tinode@postgres LOG:  connection authorized: user=tinode database=postgres
2023-10-08 11:44:37.408 PDT [850077] tinode@postgres LOG:  connection authorized: user=tinode database=postgres
2023-10-08 11:44:37.409 PDT [850079] [unknown]@[unknown] LOG:  connection received: host=127.0.0.1 port=42878
2023-10-08 11:44:37.409 PDT [850078] [unknown]@[unknown] LOG:  connection received: host=127.0.0.1 port=42876
2023-10-08 11:44:37.410 PDT [850080] [unknown]@[unknown] LOG:  connection received: host=127.0.0.1 port=42880
2023-10-08 11:44:37.411 PDT [850080] tinode@postgres LOG:  connection authorized: user=tinode database=postgres
2023-10-08 11:44:37.411 PDT [850079] tinode@postgres LOG:  connection authorized: user=tinode database=postgres
2023-10-08 11:44:37.412 PDT [850078] tinode@postgres LOG:  connection authorized: user=tinode database=postgres
2023-10-08 11:44:37.413 PDT [850078] tinode@postgres ERROR:  permission denied to create database
2023-10-08 11:44:37.413 PDT [850078] tinode@postgres STATEMENT:  CREATE DATABASE tinode WITH ENCODING utf8;
2023-10-08 11:44:37.414 PDT [850080] tinode@postgres LOG:  disconnection: session time: 0:00:00.004 user=tinode database=postgres host=127.0.0.1 port=42880
2023-10-08 11:44:37.414 PDT [850079] tinode@postgres LOG:  disconnection: session time: 0:00:00.005 user=tinode database=postgres host=127.0.0.1 port=42878
2023-10-08 11:44:37.414 PDT [850077] tinode@postgres LOG:  disconnection: session time: 0:00:00.007 user=tinode database=postgres host=127.0.0.1 port=42874
2023-10-08 11:44:37.414 PDT [850076] tinode@postgres LOG:  disconnection: session time: 0:00:00.008 user=tinode database=postgres host=127.0.0.1 port=42872
2023-10-08 11:44:37.418 PDT [850078] tinode@postgres LOG:  disconnection: session time: 0:00:00.008 user=tinode database=postgres host=127.0.0.1 port=42876

The database exists, I already created it, so why is it trying to create it again instead of just running migrations?

I thought maybe just run upgrade?

tinode-v0.22.10$ ./init-db -upgrade
2023/10/08 11:45:45 Database adapter: 'postgres'; version: 113
2023/10/08 11:45:45 Database not found. Creating.
2023/10/08 11:45:45 Failure: ERROR: permission denied to create database (SQLSTATE 42501)

Same issue.

or-else commented 11 months ago

The "database exists" is not checked by existence of the database, but by reading from kvmeta table, which does not exist. Then the adapter tries to drop and recreate the database but fails to do so because it does not have the permission.