A
A
Andrey Ivanov2022-03-15 21:50:22
Python
Andrey Ivanov, 2022-03-15 21:50:22

How to connect to psql on Ubuntu using aiogram library?

There is a small telegram bot written in Python using the aiogram library. I'm trying to run it on the server, but it gives me an error that it's impossible to connect to the PostgreSql database. I acted in the following order:

Installed postgresql on ubuntu:
sudo apt -y install postgresql

Then switched to the postgres user using the command:
sudo -i -u postgres

Created an additional role and gave superuser rights:

createuser --interactive
Enter name of role to add: username
Shall the new role to be a superuser (y/n)?: y


Then I created a database:
createdb username

I inserted these parameters in the bot code:
class PgDatabaseAPI:

    def __init__(self, loop: asyncio.AbstractEventLoop):
        self.pool: asyncpg.pool.Pool = loop.run_until_complete(
            asyncpg.create_pool(
                user='username',
                password='mylongpassword',   # здесь указал пароль от учетной записи ubuntu с одноименным именем username
                host='localhost'
            )
        )


But when starting the bot, an error pops up:
asyncpg.exceptions.InvalidPasswordError: password authentication failed for user "username"


What's wrong? How to connect the bot with the DB correctly? Is it possible to bind the bot directly to the default account "postgre" without creating other roles?

Only if possible in the most intelligible language)) I don’t understand either Ubuntu or PostgreSQL and I don’t fully understand how it works (this part of creating a bot, which I did according to tutorials)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2022-03-15
@TomRiddle

# here I specified the password for the ubuntu account with the same name username

The ubuntu account password has nothing to do with the user password in postgres. You didn't seem to create a password there at all.
Go to psql under username, there is a command\password

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question