postgres error when starting Phoenix server - postgresql

I Just made a new app in phoenix. Im following along with the Programming Phoenix book. I am on chapter 3 and after making the files and using:
mix phoenix.server
I get an angry red error saying:
[error] Postgrex.Protocol (#PID<0.234.0>) failed to connect: **
(Postgrex.Error) FATAL 28000 (invalid_authorization_specification):
role "postgres" does not exist
it keeps repeating this over and over.

Edit: Changes made to reflect comments.
This will delete all local databases
rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8
sudo -u <your local username> psql postgres
CREATE USER postgres SUPERUSER;
CREATE DATABASE postgres WITH OWNER postgres;
This simply creates the Postgres user
psql -U postgres
CREATE USER postgres;
Thanks #Dogbert, #mudasobwa

Related

postgres superuser issues during pg_upgrade

I'm using homebrew on Mac OSX to manage a postgres database. I'm trying to upgrade my postgres install from 11.9 to 13.0 using pg_upgrade. When I run pg_upgrade I get the following error about not being a superuser. If I try running as the "postgres" user, I get an error that the "postgres" user is not the install user.
pg_upgrade --old-datadir /usr/local/var/postgresql#11 --new-datadir /usr/local/var/postgres --old-bindir /usr/local/Cellar/postgresql#11/11.9/bin --new-bindir /usr/local/Cellar/postgresql/13.0/bin -c -U Brian
Performing Consistency Checks
-----------------------------
Checking cluster versions ok
connection to database failed: FATAL: must be superuser to connect in binary upgrade mode
could not connect to source postmaster started with the command:
"/usr/local/Cellar/postgresql#11/11.9/bin/pg_ctl" -w -l "pg_upgrade_server.log" -D "/usr/local/var/postgresql#11" -o "-p 50432 -b -c listen_addresses='' -c unix_socket_permissions=0700 -c unix_socket_directories='/usr/local/Cellar'" start
Failure, exiting
Try as the "postgres" user
pg_upgrade --old-datadir /usr/local/var/postgresql#11 --new-datadir /usr/local/var/postgres --old-bindir /usr/local/Cellar/postgresql#11/11.9/bin --new-bindir /usr/local/Cellar/postgresql/13.0/bin -c -U postgres
Performing Consistency Checks
-----------------------------
Checking cluster versions ok
Checking database user is the install user
database user "postgres" is not the install user
My system username is "Brian" and the original 11.9 database was installed as that user, but somehow it does not have superuser rights. I'm not sure how that happened but never quite realized it until now. My 13.0 database seems to be correctly set up with "Brian" as a superuser.
template1=# \du+
List of roles
Role name | Attributes | Member of | Description
---------------+-------------------------------------+------------+-------------
Brian | Create role, Create DB, Replication | {} |
When I login with psql -d template1 -U postgres and try to alter the role I get the following error.
template1=# alter role Brian with superuser;
ERROR: role "brian" does not exist
Time: 0.415 ms
If I log in as sudo -u postgres -i and try to alter or create a user, I also get the following errors:
psql -c "alter role Brian with superuser;"
ERROR: role "brian" does not exist
createuser -s Brian
createuser: error: creation of new role failed: ERROR: role "Brian" already exists
Does anyone know why postgres is confused over "Brian" and "brian" and how I can give superuser rights the role "Brian" so I can properly perform a pg_upgrade? When I tried brew postgresql-upgrade-database it initially gave me the same superuser error and now, upon rerun, it says that everything is already upgraded.
Case of SQL identifiers from within SQL is ignored except when in double quotes:
alter role "Brian" with superuser;
But when specified on the command line (with -U, for example) case is not ignored.

executing a .sql file through ubuntu command line for postgres

I have installed postgresql on ubuntu using:
$ sudo apt install postgresql
Now, I have a series of sql queries I would like to fire to create schemas and users and tables etc. I have put those queries in a .sql file as below:
$ sudo nano postgressetup.sql
CREATE SCHEMA schma;
CREATE USER a2i WITH PASSWORD 'password';
GRANT CONNECT ON DATABASE postgres TO schma;
This file has all the queries. I tried something like:
$ psql -U postgres -d postgres -a -f postgressetup.sql
and received error:
psql: FATAL: Peer authentication failed for user "postgres"
I want to know the way I can execute this .sql file.
Note: I've just installed postgres and no further operation is done on it. Any help is appreciated.
You can use the following command explicitly providing db context user
sudo -u postgres psql -U postgres -d postgres -a -f postgressetup.sql

Postgresql - postgres#localhost failed FATAL: role does not exist

I just installed postgres (with brew) and started it with pg_ctl -D /usr/local/var/postgres -l logfile start
I now try to connect to it in the terminal but get the following:
Connection to PostgreSQL - postgres#localhost failed
FATAL: role "me" does not exist
The issue might be that I first installed postgres.app and then didn't get it work right and then removed it.
You don't show what you typed and as which user you did it, but from the error message I'd say that you should add -U postgres to the psql invocation.

role "postgres" does not exist; cannot createuser

I am on Linux Mint Cinnamon 2.2.16.
During the process of getting Rails up and running, I am having problems with Postgres.
postgres#BL ~ $ psql --version
psql (Postgres-XC) 1.1
(based on PostgreSQL) 9.2.4
I was unable to get anything working under my usual username, so I changed to the default user using
sudo su - postgres
I cannot get anything to work with createuser.
postgres#BL ~ $ psql
psql: FATAL: role "postgres" does not exist
postgres#BL ~ $ createuser -s -U $USER
createuser: could not connect to database postgres: FATAL: role "postgres" does not exist
postgres#BL ~ $ sudo -u postgres createuser newname
Sorry, user postgres is not allowed to execute '/usr/bin/createuser newname' as postgres on BL.
postgres#BL ~ $ which psql
/usr/bin/psql
postgres#BL ~ $ psql \l
psql: FATAL: role "postgres" does not exist
After thoroughly researching the problem and tearing out more than a few hairs, I decided this was some variation of a problem with packaging/installation, similar problem noted here: unable to create user postgres: role "postgres" does not exists
I did a complete uninstall, as per below, and reinstalled without postgresql-xc
How to thoroughly purge and reinstall postgresql on ubuntu?
The new install had expected behavior with the user "postgres" and I was able to add myself as a superuser and create new databases. After some post-installation finagling, Rails seems to be running and playing nice with postgres.
You have to authenticate to psql as the superuser in order to manage users.
For example
psql -U root
Afterwards
create user paige with password 'paige';

How to install progresql-9.3 properly

I followed the installation on this document http://www.postgresql.org/download/linux/ubuntu/ and every thing seems to be fine. But when I try to this follow this tutorial: http://www.postgresql.org/docs/9.3/static/tutorial-createdb.html, everything is not fine any more. This is the error I got when I try to create a database table:
$ createdb mydb
WARNING: password file "/home/.../.pgpass" has group or world access; permissions should be u=rw (0600) or less
WARNING: password file "/home/.../.pgpass" has group or world access; permissions should be u=rw (0600) or less
createdb: could not connect to database template1: FATAL: role "..." does not exist
$ /usr/local/pgsql/bin/createdb mydb
bash: /usr/local/pgsql/bin/createdb: No such file or directory
You got one warning and one error.
You can handle the warning by this command of line in your terminal:
$ chmod 600 ~/.pgpass
When you write "psql" in your terminal, Postgres DBMS try to connect to the one database with your computer name but it can't find it. In other hand Postgrest create a database named "postgres" when it install so try connect to this and create your database. You can connect to "postgres" database easily with this command:
$ psql postgres
If you get the "connections on Unix domain socket "/tmp/.s.PGSQL.5432"?" error start your database with this command:
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Check the result with this command:
$ pg_ctl -D /usr/local/var/postgres status
The error message tells you what's wrong. Change the permissions on .pgpass:
chmod u=rw $HOME/.pgpass
Also, unless you edited the error message and didn't say so, you appear to actually be attempting to connect as user .... I imagine the examples you read said something like:
psql -U "..." template1
and assumed you'd replace "..." with the actual username.