I’ve installed postgres on manjaro, then I’ve created the user wiki with password wiki.

❯ sudo -iu postgres
[postgres@arch-desktop ~]$ createuser -P wiki
[postgres@arch-desktop ~]$ psql
postgres=# \du
Role name
wiki

And when I try to run this program I get the error

thread 'main' panicked at 'Connection Error: error returned from database: database "wiki" does not exist', src/main.rs:53:9

According to this

// Change this according to your database implementation,
// or supply it as an environment variable.
// the database URL string follows the following format:
// "protocol://username:password@host:port/database"
const DATABASE_URL: &str = "postgres://wiki:wiki@localhost:5432";

it shouldn’t be using wiki as the database name.

In the tutorial, why is it creating the database after it’s already connected to it? Shouldn’t it check if the database exists and create it only if it doesn’t?

If I create the database manually with

[postgres@arch-desktop ~]$ createdb media_wiki_db
postgres=# ALTER DATABASE media_wiki_db OWNER TO wiki;

and use this instead

const DATABASE_URL: &str = "postgres://wiki:wiki@localhost:5432/media_wiki_db";

I get the error

thread 'main' panicked at 'Execution Error: error returned from database: cannot drop the currently open database', src/main.rs:53:9

thread ‘main’ panicked at ‘Execution Error: error returned from database: permission denied to create database’, src/main.rs:53:9

If I use the postgres database

const DATABASE_URL: &str = "postgres://wiki:wiki@localhost:5432/postgres";

I get

thread 'main' panicked at 'Execution Error: error returned from database: permission denied to create database', src/main.rs:53:9

Probably missing CREATEDB permission

❯ sudo -iu postgres
[postgres@arch-desktop ~]$ createdb test
postgres=# ALTER USER wiki CREATEDB;
postgres=# ALTER DATABASE test OWNER TO wiki;
postgres=# \l
\q
Ctrl+D

Changed to const DATABASE_URL: &str = "postgres://wiki:wiki@localhost:5432/test";

❯ cargo run
thread 'main' panicked at 'Connection Error: error returned from database: database "test/media_wiki_db" does not exist', src/main.rs:53:9

What am I doing wrong? How do I connect to the database? Actually what I would like to do is drop the database, recreate it and connect. At least for now.

@OptimusPrime@lemmy.ml
creator
link
fedilink
2
edit-2
2M

I had to solve this by using two databases

const DATABASE_URL: &str = "postgres://admin:1234@localhost:5432";
const POSTGRES_DATABASE_URL: &str = "postgres://admin:1234@localhost:5432/postgres";

since I couldn’t drop the database while using it. So in the end I connect first to the postgres database and then refresh and connect to the other database.

Solution context

Linux Admin
!linux

    Ask specific questions about how to do something with linux

    General linux discussion on lemmy.ml

    Create Post From:
    lemmy.ml

    Community icon by Alpár-Etele Méder,
    licensed under CC BY 3.0

    • 0 users online
    • 2 users / day
    • 3 users / week
    • 3 users / month
    • 8 users / 6 months
    • 37 subscribers
    • 8 Posts
    • 13 Comments
    • Modlog