Postgresql setup - ruby

I'm learning Ruby and Postgresql and I'm trying to run a db project where I'm running this file to get started:
#!/usr/bin/env sh
dropdb sqlzoo
createdb sqlzoo
psql sqlzoo < data/create_tables.sql
But I get this output in the console:
dropdb: could not connect to database template1: FATAL: role "my_name" does not exist
createdb: could not connect to database template1: FATAL: role "my_name" does not exist
psql: FATAL: role "my_name" does not exist
When logging into PSQL it shows that user <my_name> is a Superuser. The template1 db is there by default.
How can I proceed here to get this setup working?

Related

no predefined role when I copy postgres from one computer to another (both are windows OS)

I copied postgres from one windows computer to another (also windows) and start postgres on the latter one as a service (where "C:/PostgreSQL/data" is a new directory to store data), with the following command
pg_ctl.exe register -N PostgreSQL -D "C:/PostgreSQL/data" –S auto
But when I tried to create a server with username "postgres" it replies me that user postgres does not exist
I tried to connect to postgres or create a user by the following command (my username under windows is cdeguide), but I had no successs. Could you give me a way to create a postgres role
C:\Program Files\PostgreSQL\13\bin>psql -U postgres
psql: error: FATAL: role "postgres" does not exist
C:\Program Files\PostgreSQL\13\bin>psql
psql: error: FATAL: role "cdeguide" does not exist
C:\Program Files\PostgreSQL\13\bin>psql -U root
psql: error: FATAL: role "root" does not exist
C:\Program Files\PostgreSQL\13\bin>createuser -s postgres
createuser: error: could not connect to database template1: FATAL: role "cdeguide" does not exist
C:\Program Files\PostgreSQL\13\bin>createuser -s cdeguide
createuser: error: could not connect to database template1: FATAL: role "cdeguide" does not exist
Start the cluster in single-user mode and query pg_authid for the bootstrap superuser:
SELECT rolname FROM pg_authid WHERE oid = 10

Running mix ecto.create command for creating a database in a Phoenix project with a postgresql instaled by brew

I have installed postgres using package manager (brew). After starting the service as background and run this to create database on Phoenix I got this error:
dedeco#MacBook-Pro-Dedeco> mix ecto.create
23:57:56.112 [error] GenServer #PID<0.242.0> terminating
** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) role "postgres" does not exist
(db_connection) lib/db_connection/connection.ex:87: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for Hello.Repo couldn't be created: killed
If you installed Postgres using homebrew, it probably was created a SUPERUSER using your operating system (OS) USER, like your name.
So you need a postgres user, you must create using these commands:
dedeco#MacBook-Pro-Dedeco> psql postgres
psql (12.2)
Type "help" for help.
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
dedeco | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres=# CREATE USER postgres SUPERUSER;
CREATE ROLE
postgres=#
Another option is changing the postgres user int the file config/dev.exs (or prod.exs, it depends which env you are) as follow:
# Configure your database
config :hello, Hello.Repo,
username: "<USER_NAME>",
password: "<PASSWORD>",
database: "hello_dev",
hostname: "localhost",
show_sensitive_data_on_connection_error: true,
pool_size: 10

psql: error: could not connect to server: FATAL: password authentication failed for user

I have successfully installed postgresql and added path to my environment variable in windows 10. But the problem is when i try to run psql postgresql in command prompt it gives error saying
C:\Users\adity>psql postgres
Password for user adity:
psql: error: could not connect to server: FATAL: password authentication failed for user "adity"
I am 100% sure my password is correct I have tried reinstalling and uninstalling many time in case i missed password but every time it gives me same error. Although when i try to run from GUI it starts running. This is frustrating and I am not sure what the problem is.
The database superuser that was created during database cluster creation is very likely called postgres.
So rather than using the default, which is to use the database user whose name is the same as your current operating system user, explicitly specify the database user postgres:
psql -U postgres

PostgreSQL keeps prompting for a password I never gave in PowerShell

I'm trying to set up postgresql to run locally on my windows machine. I've downloaded psql from postgresql.org and installed it. However, whenever I try to run the psql command from the command line it prompts me for a password that I never gave it for a user it seems to have automatically generated based on the user name of my computer.
In Powershell:
PS C:\Users\Theophilus> psql
Password:
psql: FATAL: password authentication failed for user "Theophilus"
Nothing I would use as a password works in this case. I've uninstalled and reinstalled postgres from my windows machine to see if maybe there was somewhere to input a password but I found nothing.
Additionally, running psql with the default username of 'postgres' results in the same problem.
PS C:\Users\Theophilus> psql -U postgres
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
Hopefully someone can help me.
That's because you're not providing a username in the command so it's using your current username that you're logged in as which doesn't have permission. You should use the postgres account.
c:\path\to\psql.exe -U postgres
So in your case
PS C:\Users\Theophilus> psql -U postgres
https://wiki.postgresql.org/wiki/First_steps

Could not connect to database postgres: FATAL: role"_postgres" does not exist

Working on OS X 10.10, installed postgreSQL and PostGIS from
here, psql vers 9.3.5. I am having a hard time getting postgreSQL running.
I installed the packages as admin on my computer. My username is christoph
The only way I can log in is via:
$ psql -U postgres
I want to create user called christoph, as my admin name on the computer.
I tried (from the terminal, without being "logged in into psql"):
$ sudo -u postgres createuser christoph
> sudo: unknown user: postgres
Then I read a little and tried (from the terminal, without being "logged in into psql"):
$ sudo -u _postgres createuser christoph
> Password: ****
> Could not connect to database postgres: FATAL: role "_postgres" does not exist
Why isn that working?
On recent version of OS X and with some installation methods the system user is created with a '_' prepended or appended to 'postgres', so the name of your system user is postgres_ or _postgres in your case, not 'postgres'. I don't use OS X, so I don't know what drives them to do this. Seems like they want to adhere to a naming schema for system accounts.
Not to be confused with the Postgres DB user (login role) of the name postgres. This mismatch causes all sorts of confusion. At least people become aware of the different meaning of some syntax elements ...
That's why you can log into Postgres via:
$ psql -U postgres
postgres is the DB role here, not the OS user.
But this won't work:
$ sudo -u postgres
Because there is no OS user of that name. Try instead:
$ sudo -u _postgres
But then peer authentication still won't work, because there is no DB user of the same name _postgres. Related answer:
Postgres user does not exist?
The authentication method activated by default in standard installations is peer authentication, where a system user on the local system has password-less access to a database role of the same name. That explains the last error message:
Could not connect to database postgres: FATAL: role "_postgres" does not exist
Your system tried to log into the DB with the same name as your current OS user using peer authentication, which fails due to the naming mismatch.
Your last command should work like this:
$ sudo -u _postgres createuser -U postgres christoph
The added -U postgres is an option to createuser specifying the DB role to log in with.
You still have to enter the password. I would consider using an entry in the a .pgpass file for password-less access, while the system user is different from the supposedly associated DB role.
Related:
Login Failed with Existing User on PostgreSQL
Run batch file with psql command without password

Resources