DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far...

Welcome! This is a Q&A website for computer programmers and users alike, focused on helping fellow programmers and users. Read more

What are you stuck on? Ask a question and hopefully somebody will be able to help you out!
+1 vote

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.

by
edited by
+1

In the tutorial, why is it creating the database after it's already connected to it?

In the tutorial it's establishing a connection to the server, dropping the database (if it exists), and finally recreate it.

and use this instead
const DATABASE_URL: &str = "postgres://wiki:wiki@localhost:5432/media_wiki_db"

You cannot drop a database you have an open connection to. This is also explained in the documentation: "It cannot be executed while you are connected to the target database. (Connect to postgres or any other database to issue this command.) Also, if anyone else is connected to the target database, this command will fail unless you use the FORCE option.

permission denied to create database

This is self-explanatory. You need to grant your user permissions for dropping/creating databases.

If you create the database manually, you should not have to run any script that tries to create or drop the database. I do not have experience with this ORM in particular but, if you have created the database manually, you can just connect to it and the ORM should create all the relevant tables into it.

1 Answer

0 votes
 
Best answer

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

by
edited by
Contributions licensed under CC0
...