Not sure if I have PostgreSQL installed on my Mac properly for creating Flask web app - postgresql

I installed PosgreSQL 10.5 on a Mac running macOS 10.13.6 using Homebrew by following this tutorial. After installation, I ran =# \du to view all users. I saw the user with my Mac's name that the tutorial says PostgreSQL creates during installation, but the "postgres" default user was not created (or at least did not appear).
My Main Issue is...
I need to login to PostgreSQL 'at the root', and I either do not understand exactly what that means, or something is wrong. In following the very first steps from the PostgreSQL wiki's First Steps documentation, it says to...
First connect/login as root:
# su - postgres
$ psql
psql (9.6.0)
Type "help" for help.
But when I open a terminal window and type in su - postgres it asks for a password. I've typed in every password I could possibly have given that role, and it just says su: Sorry.
I did some research and tried to change the password by doing this:
postgres=# \password postgres.
That prompted me to enter a password twice, and it seemed to work. But, when I go back and run: su - postgres, it just gives me the same su: sorry.
So I tried typing in: psql postgres. This at least changed the $ to postgres=#, which tells me that I am at the PostgreSQL command line, and then the same: su: postgres, but that just changed the (=) sign to a (-), so now it changed to: postgres-#
I thought that maybe this meant that I was "logged into PostgreSQL at the root, but then I ran the following:
postgres=# su - postgres
postgres-# CREATE SCHEMA test
postgres-# CREATE USER tester PASSWORD 'P#ssword1'
postgres-# GRANT ALL ON SCHEMA test TO tester
postgres-# \q
...and none of that worked. It did not create a user or a schema.
History...
I've installed/uninstalled PostgreSQL on this Mac several times
in the past for testing
I've also installed/uninstalled pdAdmin for various reasons
It is highly possible that I did something in all of that shuffle that brought about this issue with the 'postgres' role not being created during this Homebrew instal. So, I did read through the rest of that tutorial and I did type in $ createuser postgres and that did seem to create the 'postgres' role. I tried to use ALTER ROLE to give that role Create DB but I never could get it to work using the command line, so I just opened pgAdmin, saw the role, granted it Create DB, and then back in command line ran =# \du again, and Create DB did appear next to the 'postgres' role I'd created. I added everything in pgAdmin, and now it looks like this.
List of roles
Role name | Attributes | Member of
----------------+------------------------------------------------------------+-----------
MyMacsNameHere | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres | Superuser, Create role, Create DB, Replication | {}
My end goal...
I am trying to follow along with this YouTube tutorial about creating a Flask web application from scratch. In the YouTube tutorial, he uses MySQL, but I want to use PostgreSQL. Needless to say, I am up against a learning curve.

Related

How to reset password in postgresql (psql) for particular role in case when I list the role name it is not exist?

I have installed PostgreSQL for a long time but just currently learning it.
Here is what happened if I run psql in the command prompt
C:\Users\VandaRsq>psql
Password for user Vanda Rashq:
Since I forgot the password for the Vanda Rashq role but I remember for the postgres role, I run psql -U postgres.
I tried to list the role by using du command and the result is this:
I also tried using SELECT rolname FROM pg_roles command and yield:
I have tried to follow this tutorial and do ALTER USER "Vanda Rashq" WITH PASSWORD 'new_password'; but it returns ERROR: role "Vanda Rashq" does not exist
My question is, does the "Vanda Rashq" role actually still exist? If yes, how to reset (change) the password in case I forgot the password? If not, how to change the default role when running psql to postgres role
Notes: I have tried to uninstall the PostgreSQL and remove all of the directories but when I try to run psql, it still ask Password for user Vanda Rashq
If the user you're looking for is not listed after calling \du in psql then the user does not exist in the database.
Btw, you could also use a select to retrieve information about database users: select * from pg_catalog.pg_user;
EDIT:
Like #jjanes pointed out you get challenged for a password based on the USER configuration in yourpg_hba.conf (see docs).
For authentication method peer it is stated:
Obtain the client's operating system user name from the operating system and check if it matches the requested database user name.

Fail to connect PostgreSQL DB from my Linux User

I am new to technologies , please do not judge my question too strong :).
I installed in My Ubuntu 18.04 PostgreSQL 10.7. To be able to enter my DB I need to enter the following commands from my terminal. sudo -u postgres psql.
Is there any shortened way where I can connect it from my Ubuntu User account. For example. if I input psql it will open database environment where I can type PostgreSQL commands.
Thank you.
Just execute this command in your terminal :
alias psql='sudo -u postgres psql'
So the next time, you input psql and execute, you will be in database environment.
I see two options:
1) Create alias for this command sudo -u postgres psql .
2) Go to psql and create new superuser and database for it:
CREATE ROLE username SUPERUSER;
ALTER ROLE username WITH LOGIN;
CREATE DATABASE username;
You shouldn't be using the superuser account for your normal database work. That is as if you were using root for everything in Linux.
You need to create a regular user with the privileges to create or modify tables in your database. This can be done by granting the user all privileges on the database (which is not the same as making that user a superuser) or make that user the owner of that database.
As documented in the manual psql tries to connect to a database with the name of the current Linux user and with a database user with the name of the current Linux user. So if you want to keep things simple create a user with your regular Linux user's name and an database that is owned by that user:
create user rob password 'somepassword';
create database rob owner = rob;
Assuming your Linux user is rob, then all you need to do is:
psql
and you are connected to a database where you can create and manage tables.
Depending on how you installed Postgres, you might need to adjust pg_hba.conf to allow rob to log in directly.
Again: please do NOT use the superuser account for your normal work.

createdb: database creation failed: ERROR: permission denied to create database

I am pretty much confused about root user,super user,user and permissions! I am not able to create a database inside user "athleticu". Following are the commands I used:-
athleticu#ip-172-30-4-103:/home/ubuntu$ createdb -T template0 simple_db1
createdb: database creation failed: ERROR: permission denied to create database
athleticu#ip-172-30-4-103:/home/ubuntu$ sudo createdb -T template0 simple_db1
sudo: unable to resolve host ip-172-30-4-103
createdb: could not connect to database template1: FATAL: role "root" does not exist
Please somebody clarify my doubts and tell me what should I write!
Hey I have already solved this. What you have to do is to first login as postgres user as follows:
$ su postgres
$ psql
postgres=# alter user athleticu createdb;
ALTER ROLE
Hope it helps you :)
Type \du in psql and you will see a list of all the registered users and what type of privileges each one has.
In order to grant privileges to the user which is logged in (eg 'user1'), I had to sign out and log in using one of the superuser roles in that list (eg. 'user2'), using the following command:
psql -U 'user2' -h localhost 'database2'
where 'database2' is the name of the one that specific superuser 'user2' has privileges to.
Once you are logged in as a superuser, you can grant privileges to 'user1' by:
ALTER ROLE user1 WITH CREATEDB
or
ALTER ROLE user1 WITH SUPERUSER
Then sign in again as user1, who is now a superuser.
This blog was helpful as well as this link.
Currently, this worked for me:
sudo su postgres
psql
ALTER USER username WITH CREATEDB;
\q
exit
The root user is an account on the system independent from Postgres. There is only one root user.
A superuser is an account in Postgres with access to everything. There may be many superusers.
System accounts and Postgres accounts are different things, although unless you specify a Postgres username when you connect to the database (through utilities like psql, createdb, dropdb, or otherwise), it will use the current system user's name in hopes that there is a corresponding Postgres account with the same name. The root user does not, by default, have a corresponding account in Postgres.
When you install Postgres on *nix, it creates both a superuser named postgres and a system user named postgres.
Therefore, when you need to do something with Postgres as the built-in superuser, you have two options:
You may sudo su - postgres to become the postgres system user and execute your command (createdb, psql, etc). Because the system user has the same name as the database superuser, your command will connect as the appropriate account.
You may specify the username to execute as with the -U switch, eg psql -U postgres ....
Depending on your Postgres server's authentication settings, you may be required to enter a password with either or both connection methods.
What you can do when you have fresh installation of PostgreSQL is create your user with some rights (see createuser documentation):
my-user> sudo su - postgres -c "createuser <my-user> --createdb"
This will allow my-user to create DBs just like so:
my-user> createdb <my-db>
If you want the my-user to be able to do anything just use the --superuser flag instead:
my-user> sudo su - postgres -c "createuser <my-user> --superuser"
I got the same error and I found out that the reason was that I was trying to create a database outside of psql as a user which did not exist for postgresql. I found out about it and solved it by taking the following steps:
In my terminal I logged in as postgres user (the root user by default for postgresql) by typing sudo -u postgres psql
While inside the psql I typed \du to see all users and their privileges. I found out that I had only one user (the postgres one) and I had to create another superuser which had the same username as my Linux user (george)
I typed (still inside psql) CREATE USER george SUPERUSER; and this way I created a new super user called george.
I exited psql (by typing \q) and I was now able from outside psql, meaning from my terminal, to run created db <database name> with no issues at all.
Error ? You are trying to perform database actions( Creating Database, creating Roles) using a user that doesn't have the permission for those types of actions you are trying to perform.
solution ? Simply login to your database on the command line, i.e for PostgreSQL one will use "sudo -u postgres psql", then confirm that users specific assigned roles using the command "\du", most probably he/she doesn't have the necessary permissions to perform the actions you wanted. Then simply assign the roles you want the user to perform ,i.e create Database or simply make user "Superuser" by following along(https://chartio.com/resources/tutorials/how-to-change-a-user-to-superuser-in-postgresql/)

Postgresql: FATAL: role does not exist [duplicate]

This question already has answers here:
psql: FATAL: role "postgres" does not exist
(32 answers)
Closed 20 days ago.
I'm setting up Postgresql for use with a Rails app, but I do not seem to be able to connect to, or properly configure, the database (the errors I get after starting the Rails server are: ActiveRecord::NoDatabaseError and could not translate host name "MyProfile" to address: nodename nor servname provided, or not known).
I gather from the dozens of other questions on this topic that I need to switch to, or create the "MyComputer" role, however I've noticed that they all require using the psql command. When I run even just psql I, again, get the error FATAL: role "MyProfile" does not exist.
So far I've been following Heroku's instal process and am stuck here (or more accurately here, after the installation, where Heroku says that running psql -h localhost should work). Am I missing an obvious step here/doing something wrong?
I've also tried:
sudo su - MyProfile
variations of sudo -u postgres createuser owning_user
and a couple other commands in an effort to create this roll/user, but I can't seem to get done what I need to to resolve the issue.
EDIT
I've also run ps aux | grep postgres and it looks like all the PID's that are associated with anything postgres are running under "MyProfile" (assuming I'm reading it right). But the psql command still returns that the role does not exist. #sadface
EDIT 2
I just opened the Postgres App and clicked the "Open psql" button. It opened the Terminal, ran a command ('/Applications/Postgres.app/Contents/Versions/9.5/bin'/psql -p5432) and then gave me the same error (psql: FATAL: role "MyProfile" does not exist). Which perhaps suggests to me that it's an system issue, and not a Rails issue at all?
Edit 3
This is most certainly a pg issue, not a rails issue. I just uninstalled the app, reinstalled it using SQLite (yucks), ran the local server and got the test landing page to show up. So the problem appears to be with my local machine but not the app itself. Removed RoR tag, and still looking for answers from Postgres gurus on why the role issue persists :)
I ran into similar issues when setting a new Rails application with Postgresql. I got the following error messages below
FATAL: role "promisepreston" does not exist
Couldn't create 'MyBlog_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: FATAL: role
Caused by:
PG::ConnectionBad: FATAL: role "promisepreston" does not exist
To solve this simply follow the solution below
First, we need to login to the postgres user account via the command line interface;
sudo su - postgres
Next, connect to the database server using the psql client, as the postgres role:
psql -U postgres
Welcome to psql 10.6, the PostgreSQL interactive terminal.
postgres#Preston-PC:~$ psql -U postgres
psql (10.6 (Ubuntu 10.6-0ubuntu0.18.04.1))
Type "help" for help
postgres=#
Next, connected with the psql client, we’ll create a role with our desired rolename that has the LOGIN attribute and our desired password, and that can create databases and manage roles (N/B: Please do not type this postgres=#, since it's a placeholder):
postgres=# create role rolename with createdb login password 'password1';
Note the required trailing semicolon ( ; ) at the end of the SQL statement. The single-quotes ( ‘ ‘ ) are not part of the password, but must enclose it.
Did it work? You can check using \du command (N/B: Please do not type this postgres=#, since it's a placeholder):
postgres=# \du
You can now run the command to create the database for your Rails application;
rails db:create
And then also run the command to migrate the database for your Rails application;
rails db:migrate
That's all.
I hope this helps
What a mess...one thing that I forgot to mention that probably would have been astronomically helpful to the community is that I'm taking over someone else's machine. There were lingering settings that just decided they weren't going to play nice.
At the end of the day, I got rid of Postgress.app, installed postgres with Homebrew, and the balance of what I needed was here: http://exponential.io/blog/2015/02/21/install-postgresql-on-mac-os-x-via-brew/
Specifically the line that saved me was createdb 'whoami' (see the actual post...the syntax is a bit different than what I just wrote because of stackoverflow formatting)...in retrospect it seems obvious, but it helped my current logged in user overcome the presets of the other legacy user by actually creating the database that the psql the whole setup was looking for.
Lesson learned!
Thanks for the help #max, helped me avoid another issue I was about to cause as well!
If you are using Postgres.app you should leave out the username and password from config/database.yml. Also that error is telling you that you have entered MyProfile not as the user for (or role in PG parlance) but as the host for the database connection (instead of localhost).
This is really all you need in your config/database.yml to get Postgres.app running:
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
Its often a good idea to leave it stripped down and let the fool who absolutely has to have a password on his dev postgres server use the DATABASE_URL env var.

Postgresql Ubuntu strange behaviour (version incompatibility?)

I'm trying to run a postgresql Database on Ubuntu 16.04 LTS.
First I installed postgres using sudo apt-get install postgresql which installed version 9.5.1. I then created another user and a new database. I've granted all privileges for the new database to new new user and set the owner to the new user also.
I connected to the new database and filled it by restoring an plain backup (dump) I've created from another database (which has postgresql version 9.2) by using \i /path/to/dump.sql. I got no errors, and when I then typen \dt I got the list with the tables.
The problem is: When I now disconnect (\q) and re-connect (same as I connected before, sudo psql -U "username" dbname) and type \dt again, it says "No relations found". When I try to fill it again, I get a bunch of errors like "Relation relationname already exists".
Another problem/symptom appears when I try to use pgAdmin (installed via sudo apt-get install pgadmin3). When trying to connect using localhost, I can't connect using the newly created user (which which I can connect from the terminal). But I can connect using postgres with the password I set via Terminal. I do not see any DB I manually create from command line from pgAdmin tho.
So yeah, my guess is that at least one problem may be version incompatibility between 9.2 and 9.5. Is there any way to test/fix this? But I also think there should be other problems.
What you describe may happen if the SQL dump contains a SET search_path TO...command that sets it to a different value than what your user has by default.
So not only it will create its tables and other objects in that schema, but it will leave that search_path for the rest of the session, so when you do \dt in that same session, it does see and list the newly created tables.
But when you quit and reenter psql, this search_path is not longer in effect, you're back to the default search_path of your user, which supposedly doesn't reach the schema, so \dt no longer "sees" any table.
You can use show search_path to check this setting in a psql session, and grep "SET search_path" in the SQL file to check what it's being set to.
Per comment, it appears to be the case: the dump creates the tables into a schema that is outside of the user's default search path.
A possible solution for that user to work seamlessly would be to update its search path so that it always reaches this schema first. This can be done with:
ALTER USER username SET search_path TO schema1,"$user",public;
where schema1 is what the SQL dump refers to and where it created the tables.