0

I'm new to Postgres and I am trying to implement it through the command line. I just used it previously in a project, but that was by downloading Postgres.app and Postico. I wanted to go through the process of setting up a database through the command line. I installed postgres using Homebrew, and that was fine. I'm trying to follow this tutorial:

https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb

For some reason, none of the wrapper commands work for me. I wasn't able to use createuser, but running CREATE ROLE patrick WITH LOGIN PASSWORD 'quoted password' was fine.

When I run the \du command, I get the following:

     Role name     |                         Attributes                         | Member of 
-------------------+------------------------------------------------------------+-----------
 patrick           | Create DB                                                  | {}
 patrickshushereba | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

Which I believe is what I want. Because I created an account that isn't a superuser account.

I also ran \list to make sure that the databases that I wanted to use were created correctly.

  List of databases
        Name        |       Owner       | Encoding | Collate | Ctype |            Access privileges            
--------------------+-------------------+----------+---------+-------+-----------------------------------------
 bridgeworq         | patrick           | UTF8     | C       | C     | =Tc/patrick                            +
                    |                   |          |         |       | patrick=CTc/patrick
 bridgeworq_testing | patrick           | UTF8     | C       | C     | =Tc/patrick                            +
                    |                   |          |         |       | patrick=CTc/patrick
 postgres           | patrickshushereba | UTF8     | C       | C     | 
 template0          | patrickshushereba | UTF8     | C       | C     | =c/patrickshushereba                   +
                    |                   |          |         |       | patrickshushereba=CTc/patrickshushereba
 template1          | patrickshushereba | UTF8     | C       | C     | =c/patrickshushereba                   +

The first two in the list are the ones that I want to connect to. I'm using Knex.js and node.js to build my backend.

Here is the development object inside of my knexfile:

development: {
    client: 'pg',
    connection: {
      filename: 'postgresql://localhost/bridgeworq'
    },
    migrations: {
      directory: './data/migrations'
    },
    seeds: {
      directory: './data/seeds'
    }
  }

My problem is that when I try to run my migrations, I get the following error:

error: database "patrickshushereba" does not exist.

I haven't found any good resources to troubleshoot what is wrong. What else should I be doing in the command line to make sure that the user that I want to use patrick is connected to the database that I want to use? And if anyone is by chance familiar with Knex, is that the right way to connect to the database? Or do I need to be using a different path? Sorry for the long winded amateur question. But I'd really like to learn how to do this, and what the workflow is for setting stuff up, and I wanted to be thorough. Any help or resources would be greatly appreciated.

4
  • The good news is that you are already successfully connecting to the server, the error message stating you can’t open the nonexistent database is coming from it. Also implies, however, that you are connecting with the wrong user, and not specifying the correct database to open. Never heard of Knex but surely it’s got some documentation. The command line psql tool is easy: psql dbname username, so in your case, psql bridgeworq patrick
    – fds
    Jan 14, 2020 at 5:32
  • Is your username on that machine "patrickshushereba"? This sort of thing frequently occurs when you try to connect with incorrect parameters (or missing ones) - the server assumes you want to connect with the username you have on that particular machine. Try logging in from the psql client with no parameters - if you get this error, you know you're on the right track! p.s. welcome to the forum! :-)
    – Vérace
    Jan 14, 2020 at 6:36
  • @ fds @Vérace thank you both for your help! After reading your comments and looking at it with fresh eyes, I was trying to connect to the database with the wrong information. Obviously you guys knew that, but I didn't know how it was wrong. But I was able to straighten the problem out and I could run my migrations. So things are working for now.
    – cpppatrick
    Jan 15, 2020 at 4:52
  • You might like to mark your own question as correct - this might help those with similar issues in the future? You won't get any points though - but keep coming back and asking questions!
    – Vérace
    Jan 15, 2020 at 5:10

1 Answer 1

0

It turned out that I was creating the database and the user correctly. What I was doing wrong was not connecting to my database correctly through the Knexfile.

I looked at the docs from http://knexjs.org/

Once I adjusted the object in my knexfile to specify the database, I was able to run my migrations successfully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.