Unable To Run PSQL - postgresql

Whenever I try to run 'psql' I receieve the following error message:
ahcarpenter#ubuntu:~$ psql
psql: FATAL: role "ahcarpenter" does not exist
Any ideas on how to fix this?

That happens because
$ psql
is equivalent to
$ psql ahcarpenter -U ahcarpenter
As that user does not exist enter as user postgres
$ psql -U postgres
Once inside create the user and the database "ahcarpenter".
create user ahcarpenter;
create database ahcarpenter with owner ahcarpenter;
exit
\q
Reenter
$ psql

Related

How to restore psql DB using the different ownership

I am taking pg_dump with --no-owner permission and trying to restore it in another deb.
I took pg_dump using below command and restore it in another db using the psql command.
pg_dump dbname > dname.sql
psql -d dbname -f filename
When I try to access the db I got
"ERROR: must be owner of function *** (SQLSTATE 42501) " .
Then I noticed the DB table ownership is postgres instead of myuser. I tried to restore it using pg_restore as I used .sql format it is not allowing. Then I tried with psql -U myuser dbname -f filename. I got below error message, though the user id is available.
psql: error: FATAL: Peer authentication failed for user "****"
How do I correct it while taking the dump or while restoring how to include the user1 permission?

How to run a postgres command: could not identify current directory

I am able to run psql by doing the following:
Davids-d david$ psql --u postgres
Password for user postgres:
psql (9.4.18)
Type "help" for help.
postgres=#
However, when I run the following command, I get an error:
Davids-iMac:datadocs david$ sudo -u postgres psql -f resources/postgresql/initdb.sql
could not identify current directory: Permission denied
What does this mean, and how would I resolve this? Note that I do have the following var set:
david$ echo $PGDATA
/Users/david/PostgreSQL/data/pg94
The issue is the sudo -u postgres.
Your shell is running as you, but you're running the command as the postgres user. It does not have permission to see the file or even be in the current directory.
We can eliminate psql from the equation by just trying to read the file as the postgres user with sudo -u postgres cat resources/postgresql/initdb.sql. You should get the same error.
There's two things you have to do...
cd to a directory that the postgres user can be in.
Put the file in a place the postgres user can access.
/tmp, for example.
Your command seems wrong, try this:
sudo psql -U postgres -f resources/postgresql/initdb.sql

FATAL: database "pgdb" does not exist

I was trying to execute the command
psql -d pgdb -U nikiesh
It gives below error:
FATAL: database "pgdb" does not exist
It means that the database "pgdb" does not exist.
psql -l | grep pgdb
use this if you are unsure of the whole name of the db. If your using linux.

Attempting to run pg_dump as user "postgres" returns error related to non-postgres user

When I try to run pg_dump as the user "postgres",
$ sudo -u postgres pg_dump <pg_dump arguments>
I receive the error
psql: FATAL: role "username" does not exist
where "username" is my OS username (i.e., $USER). It seems like if I were attempting to use pg_dump as myself, it would matter that there's no "username" role, but since I'm not, it shouldn't. How do make pg_dump do something? Postgres 9.4.8 on Fedora. Thanks in advance!
If you are running pg_dump, why does the error message say psql?
I'm guessing you are doing something like this:
sudo -u postgres pg_dump foo bar $(psql blurb blurgle)
or possibly this:
sudo -u postgres pg_dump foo bar | psql blurb blurgle
So you need to sudo -u postgres for both commands.
In any case, it sounds like you are looking in the wrong place for the problem.

Entering a .sql file into a new postgreSQL database

I am having troubles getting started with psql. I can login using the script below
myusername#ubuntu:~/Desktop/dbscripts$ sudo su - postgres
postgres#ubuntu:~$
But, once here, I cannot figure out how to find my .sql file.
I tried the options supplied by Bolo here:
How to import existing *.sql files in PostgreSQL 8.4?
But they only give
myusername#ubuntu:~/Desktop/dbscripts$ psql -U root -d first -f myscript.sql
psql: FATAL: Peer authentication failed for user "root"
and
myusername#ubuntu:~/Desktop/dbscripts$ psql -f myscript.sqlp
sql: FATAL: role "myusername" does not exist
and
myusername#ubuntu:~/Desktop/dbscripts$ sudo su - postgres
postgres#ubuntu:~$ \i myscript.sql
The program 'i' is currently not installed. To run 'i' please ask your administrator to install the package 'iprint'
This can be handled from any of the above options you tried. In which path do you have myscript.sql? After you do su - postgres, give the full path . So it will be psql -d first -f <pathtosqlfile>/myscript.sql. psql -U root will not work unless you have a user root in the database. Try psql -U postgres. You can do \i sqlscript at psql prompt, not at linux command prompt as you have done. The error you are getting "role "myusername" does not exist" can be avoided either by using -U postgres (or any other db user) or by setting the PGUSER environment variable.