How to create the first DB in Postgres - postgresql

I have just installed Postgres 12 on a Mac. As you may soon appreciate I am totally new to it.
During the installation process I was asked to provide a "password". I do not remember specifically, but I think it was for the some sort of admin role.
Now I want to create my first database. Reading the documentation I insert the command
createdb myfistdb
the system asks for a password. I give the one I set during the installation processes but I got the following error
createdb: error: could not connect to database template1: FATAL: password authentication failed for user "myusername"
where myusername is the user I am logged in.
The same happens if I give the system password of myusername.
I understand that my question is pretty basic, but I have been struggling quite some time without any success, so any help will be appreciated.

The database uset name used by createdb defaults to the operating system user name, so you'll have to specify the administrative superuser explicitly:
createdb -U postgres myfistdb

Related

Postgres 13: cannot psql from CMD (incorrect password but its correct?)

Image of my info and the error itself
Yes I can access it by Shell and pgAdmin but I cannot with direct psql command...
Does anyone have any insight?
The thing that confused me was from the CMD I managed to connect as AN USER NAMED "POSTGRES" and connected to a database named "POSTGRES", for a while, I thought users and databases were one thing. Later, I thought that "POSTGRES" is like a master user and I can create many many databases with it then I found out I could create users ...
postgres=#CREATE USER me WITH PASSWORD '0000';
Add to that confusion, I managed to connect as an different user but it was still saying "postgres=#_" not a "differentuser=#" then I figured out that "postgres=#" is the database named "POSTGRES". There weren't any noob friendly tutorials on Youtube that I was able to find... I guess the pgAdmin 4 helped because I wasn't familiar yet with \du+ \l commands. I hope someone finds this useful.

Password not working after typing psql in command prompt

I'd like to learn how to use postgres, so I just installed it, set my password, and added the \bin and \lib directories to my system path. I then ran psql in the command line, typed the password that I just set when I was prompted, and now I receive this error:
psql: error: FATAL: password authentication failed for user "me"
I don't understand why that happened. Any ideas? I am using windows 10.
The windows installer asks you to specify the password for the super user named "postgres". But if you just type psql, you are trying to log in as the PostgreSQL which has the same name as your windows OS user. But that PostgreSQL user probably doesn't even exist, much less have the same password as you specified upon installation.
So the first time you log in, you have to tell it to log in as the initial superuser, with -U postgres. Once logged in, you can create a user named 'me' (create it with a password--possibly the same as the first password you assigned, although generally they would be different), and a database named 'me'. From them on, you could log in as this new user to this new database, just by typing psql and then giving the password when it asks.
In general, the initial user and the initial database are only used for maintenance operations. Other tasks should be done with the users and in the databases you set up after the first time you log in. This isn't "the law" of course, it is just "a good idea".

Fatal: password authentication failed for user "postgres" macos

I'm using PostgreSQL for the first time and I am not familiar with it. When I start pgadmin and enter master password and when I want to connect to postresql and again enter same password following error pops up:
could not connect to server: could not initiate GSSAPI security context: The operation or option is not available
could not initiate GSSAPI security context: Credential for asked mech-type mech not found in the credential handle
FATAL: password authentication failed for user "postgres"
Whats the problem here?
I just had the same issue and below is how I solved it. Because you don't have a default password for the user 'postgres', you will need to set one first, like this :
Connect to the terminal as the user 'postgres' with the current admin credential (thanks sudo)
sudo -u postgres -i
Connect to the database 'postgres'
psql postgres
(optional) If the psql command is not found, you can the PostgreSQL bin path to your $PATH like this :
export PATH=$PATH:THE_PATH_TO_POSTGRESQL_BIN_FOLDER
# Example : export PATH=$PATH:/Library/PostgreSQL/12/bin
Change the password of the user 'postgres'
--- Do not forget to my_password with the desired password
ALTER USER postgres PASSWORD 'my_password';
In the pgAdmin windows, try to connect with this new password, it should work.
It is ridiculous but my solution after many trial was very very simple.
Problem seems that password is not match or connection problem. So, first make sure your password is definitely correct. You might think you enter exact same but it might be not. I tried to wrote a password to somewhere else that i can see it is correct.Then, i copied that and paste it to setup where you put your new password. Ofcourse you have to remove postgre completely for this to try.
When setup is completed, if you tried and didn't work again, then delete Postgre server and create new one. Just enter server name, connection address and password and save. if this one also not work successfully,maybe you should check if the chosen port number is alive or not for postgre server.
Sorry for that answer is not visual and some english mistakes. I just wanted to wrote really fast. Good luck :)

createdb: permission denied to create database

I'm trying to set up a Heroku environment for python development following instructions on https://github.com/heroku/python-getting-started. When I run createdb python_getting_started:
I'm first prompted to give in a password: I entered the password of the user "postgres" in Postgres
I get an error message:
createdb database creation failed: ERROR: permission denied to create database
Don't really how to solve this one. The user "postgres" is allowed to create a database. I checked with \du that it is a Superuser and it has Create DB rights. What's going on here? Which user is Windows using to try to create a Postgres DB?
Most PostgreSQL utilities by default use your current OS session login for database connections.
You need to either set environment variable PGUSER to postgres or use createdb -U postgres python_getting_started.
You can read more about createdb parameters here, tho admittedly it does not mention default values.
EDIT: It actually does mention that it uses libpq defaults, and those are:
user
PostgreSQL user name to connect as. Defaults to be the same as the operating system name of the user running the application.

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.