Create local Postgresql account - postgresql

On OS X 10.11.2, I've installed Postgres.app and I'm running the local server. I'm trying to create a local account with a username and password so that I can develop a Rails app locally. However, running the following command:
sudo -u postgres createuser -s {USERNAME}
I receive sudo: unknown user: postgres error.
Any suggestions as to why this error occurs and how to resolve this?

sudo tells you there is no system user "postgres".
When you installed PostgreSQL, it should have created database user "postgres" and you can try use that:
$ psql -u postgres
postgres=# create user {username} password '{password}';

I managed to create a user with the following steps.
In the terminal:
createuser --superuser {USERNAME}
I then set a password in psql:
\password {USERNAME}

Related

FATAL: role "user" does not exist [duplicate]

I'm setting up my PostgreSQL 9.1. I can't do anything with PostgreSQL: can't createdb, can't createuser; all operations return the error message
Fatal: role h9uest does not exist
h9uest is my account name, and I sudo apt-get install PostgreSQL 9.1 under this account.
Similar error persists for the root account.
Use the operating system user postgres to create your database - as long as you haven't set up a database role with the necessary privileges that corresponds to your operating system user of the same name (h9uest in your case):
sudo -u postgres -i
As recommended here or here.
Then try again. Type exit when done with operating as system user postgres.
Or execute the single command createuser as postgres with sudo, like demonstrated by drees in another answer.
The point is to use the operating system user matching the database role of the same name to be granted access via ident authentication. postgres is the default operating system user to have initialized the database cluster. The manual:
In order to bootstrap the database system, a freshly initialized
system always contains one predefined role. This role is always a
“superuser”, and by default (unless altered when running initdb) it
will have the same name as the operating system user that initialized
the database cluster. Customarily, this role will be named postgres.
In order to create more roles you first have to connect as this
initial role.
I have heard of odd setups with non-standard user names or where the operating system user does not exist. You'd need to adapt your strategy there.
Read about database roles and client authentication in the manual.
After trying many other people's solutions, and without success, this answer finally helped me.
https://stackoverflow.com/a/16974197/2433309
In short, running
sudo -u postgres createuser owning_user
creates a role with name owning_user (in this case, h9uest). After that you can run rake db:create from the terminal under whatever account name you set up without having to enter into the Postgres environment.
sudo su - postgres
psql template1
creating role on pgsql with privilege as "superuser"
CREATE ROLE username superuser;
eg. CREATE ROLE demo superuser;
Then create user
CREATE USER username;
eg. CREATE USER demo;
Assign privilege to user
GRANT ROOT TO username;
And then enable login that user, so you can run e.g.: psql template1, from normal $ terminal:
ALTER ROLE username WITH LOGIN;
This works for me:
psql -h localhost -U postgres
Installing postgres using apt-get does not create a user role or a database.
To create a superuser role and a database for your personal user account:
sudo -u postgres createuser -s $(whoami); createdb $(whoami)
psql postgres
postgres=# CREATE ROLE username superuser;
postgres=# ALTER ROLE username WITH LOGIN;
For version Postgres 9.5 use following comand:
psql -h localhost -U postgres
Hope this will help.
Working method,
vi /etc/postgresql/9.3/main/pg_hba.conf
local all postgres peer
here change peer to trust
restart, sudo service postgresql restart
now try, psql -U postgres
For Windows users : psql -U postgres
You should see then the command-line interface to PostgreSQL: postgres=#
I did a healthcheck with docker-compose.
healthcheck:
test: ['CMD-SHELL', 'pg_isready']
interval: 5s
timeout: 5s
retries: 5
If you also have that change the user:
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres'] # <<<---
interval: 5s
timeout: 5s
retries: 5
In local user prompt, not root user prompt, type
sudo -u postgres createuser <local username>
Then enter password for local user.
Then enter the previous command that generated "role 'username' does not exist."
Above steps solved the problem for me.
If not, please send terminal messages for above steps.
I installed it on macOS and had to:
cd /Applications/Postgres.app/Contents/Versions/9.5/bin
createuser -U postgres -s YOURUSERNAME
createdb YOURUSERNAME
Here's the source: https://github.com/PostgresApp/PostgresApp/issues/313#issuecomment-192461641
Manually creating a DB cluster solved it in my case.
For some reason, when I installed postgres, the "initial DB" wasn't created. Executing initdb did the trick for me.
This solution is provided in the PostgreSQL Wiki - First steps:
initdb
Typically installing postgres to your OS creates an "initial DB" and starts the postgres server daemon running. If not then you'll need to run initdb
dump and restore with --no-owner --no-privileges flags
e.g.
dump - pg_dump --no-owner --no-privileges --format=c --dbname=postgres://userpass:username#postgres:5432/schemaname > /tmp/full.dump
restore - pg_restore --no-owner --no-privileges --format=c --dbname=postgres://userpass:username#postgres:5432/schemaname /tmp/full.dump
sudo -u postgres createuser --superuser $USER
sudo -u postgres createdb $USER
This should definitely work for you.
for those who using docker and correctly followed the instructions from official doc, if you still met this problem, RESTART windows and try again.
Follow These Steps and it Will Work For You :
run msfconsole
type db_console
some information will be shown to you chose the information who tell you to make: db_connect user:pass#host:port.../database sorry I don't remember it but it's like this one then replace the user and the password and the host and the database with the information included in the database.yml in the emplacement: /usr/share/metasploit-framework/config
you will see. rebuilding the model cache in the background.
Type apt-get update && apt-get upgrade after the update restart the terminal and lunch msfconsole and it works you can check that by typing in msfconsole: msf>db_status you will see that it's connected.
Follow these steps to get postgres working.
In your terminal, locate the Application Support folder with the following command.
/Users/[name of the user]/library/application support
Delete the application, Postgres.
Reinstall the app and it should work just fine.
Something as simple as changing port from 5432 to 5433 worked for me.

How do I solve this problem to use psql? | psql: error: FATAL: role "postgres" does not exist

I'm having trouble using PostgreSQL. I have recently installed this version (13+223.pgdg20.04+1) of postgresql package in ubuntu 20.04.
I'm trying to run psql command, but I get the following error:
psql: error: FATAL: role "my_username" does not exist
I have tried to create a new user with createuser me, but I get the following error:
createuser: error: could not connect to database template1: FATAL: role "my_username" does not exist
I have tried also forcing the postgres user with createuser me --username=postgres, but I get the following error:
createuser: error: could not connect to database template1: FATAL: Peer authentication failed for user "postgres"
How do I solve these problems to use PostgreSQL locally on my computer without these problems?
PD: I have reinstalled postgres and now I'm getting a different error while doing psql:
psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I'm not sure why I had a bad installation, but I have completely uninstalled postgres following this post:
https://kb.objectrocket.com/postgresql/how-to-completely-uninstall-postgresql-757
after that I have restarted my computer and installed posgres again following the proper instructions in:
https://www.postgresql.org/download/linux/ubuntu/
and now it looks like it works without problems
Peer authentication means (there are advanced possibilities, but those are not going to be used by default, while the simple method is the default for apt-installed PostgreSQL) that you have to be the OS user 'postgres' to connect as the database user 'postgres'. So you would do:
sudo -u postgres createuser me
You don't need to specify --username=postgres, since that is the default behavior anyway once you use sudo -u postgres
Alternatively, you could change your pg_hba.conf to use a different authentication method other than peer, if you want to.
You need to provide username in the psql command using -U option.
psql -U postgres
Postgresql comes with a predefined superuser role called postgres. If you want to create more roles, you first have to connect as this initial role.
first check user postgres exists:
$ id postgres
Then:
$ su - postgres
Password:
$ psql
psql (15.1 (Debian 15.1-1.pgdg110+1))
Type "help" for help.
If, Password for user postgres is no known then change it:
$ su - postgres
Password:
su: Authentication failure
$ sudo passwd postgres
New password:
Retype new password:
passwd: password updated successfully
Finally again:
$ su - postgres
Password:
$ psql
psql (15.1 (Debian 15.1-1.pgdg110+1))
Type "help" for help.

psql: sudo: unknown user "abhishek"

I have just started with Postgresql and I know that on installation, a default user postgres is created. Now I have created another role/user abhishek with:
createuser --interactive
This role has superuser permission.
So I while being logged in as postgres added LOGIN role and defined the password for role abhishek using:
ALTER ROLE abhishek WITH LOGIN PASSWORD 'pg13'
But when I try to login with this role using:
sudo -i -u abhishek
It throws the following error:
sudo: unknown user: abhishek
sudo: unable to initialize policy plugin
So any hints as to where I'm going wrong be would be welcomed.
You created a Postgres user, not a Linux user. sudo runs a Linux command under a different Linux user account. If you want to log in to Postgres using your newly created database user, tell psql that:
psql -U abhishek <name of database to connect to>

createuser command for postgres failing on windows

I installed postgresql on Windows. When I run createuser in the DOS prompt, it fails with the following error:
createuser testuser
could not connect to database postgres : FTAL: role testuser does not exist
I have tried switching the pg_hba file from md5 to trust, but that has not solved the issue. Any thoughts? The database server itself is running- I was able to connect to it using another tool. Also, the path has a reference to the postgres/bin directory.
you need to specify a super user account in order to create a user
createuser -U pgsql testuser
if you plan on using a password for this user you can use -P or --pwprompt
createuser -P -U pgsql testuser
and it will prompt you for the password.
replace pgsql with a superuser account.

How to connect Postgres to localhost server using pgAdmin on Ubuntu?

I installed Postgres with this command
sudo apt-get install postgresql postgresql-client postgresql-contrib libpq-dev
Using psql --version on terminal I get psql (PostgreSQL) 9.3.4
then I installed pgadmin with
sudo apt-get install pgadmin3
Later I opened the UI and create the server with this information
but this error appear
how can I fix it?
Modify password for role postgres:
sudo -u postgres psql postgres
alter user postgres with password 'postgres';
Now connect to pgadmin using username postgres and password postgres
Now you can create roles & databases using pgAdmin
How to change PostgreSQL user password?
You haven't created a user db. If its just a fresh install, the default user is postgres and the password should be blank. After you access it, you can create the users you need.
It helps me:
1. Open the file pg_hba.conf
sudo nano /etc/postgresql/9.x/main/pg_hba.conf
and change this line:
Database administrative login by Unix domain socket
local all postgres md5
to
Database administrative login by Unix domain socket
local all postgres trust
Restart the server
sudo service postgresql restart
Login into psql and set password
psql -U postgres
ALTER USER postgres with password 'new password';
Again open the file pg_hba.conf and change this line:
Database administrative login by Unix domain socket
local all postgres trust
to
Database administrative login by Unix domain socket
local all postgres md5
Restart the server
sudo service postgresql restart
It works.
Helpful links
1: PostgreSQL (from ubuntu.com)
Create a user first. You must do this as user postgres. Because the postgres system account has no password assigned, you can either set a password first, or you go like this:
sudo /bin/bash
# you should be root now
su postgres
# you are postgres now
createuser --interactive
and the programm will prompt you.
First you should change the password using terminal.
(username is postgres)
postgres=# \password postgres
Then you will be prompted to enter the password and confirm it.
Now you will be able to connect using pgadmin with the new password.
if you open the psql console in a terminal window, by typing
$ psql
you're super user username will be shown before the =#, for example:
elisechant=#$
That will be the user name you should use for localhost.