Cannot Run Postgres (psql) locally - macos

Whenever I try to run psql locally i.e. $ psql -d template1 -U postgres I receive -bash: psql: command not found
I've installed the Postgres Mac app, but when I click open psql I get...
Last login: Tue Jan 6 16:30:17 on ttys001
admins-MacBook-Air:~ surajkapoor$ /Applications/Postgres.app/Contents/MacOS/bin/psql ; exit;
psql: FATAL: database "surajkapoor" does not exist
logout
[Process completed]
This doesn't make sense to me. I assumed installing the mac app install postgres locally.

You need to add the directory where the psql file is to the PATH variable of the mac which would be
/Applications/Postgres.app/Contents/MacOS/bin/
Opening psql without parameters will attempt to connect to a database with the name of the current user, if you don't want to add the directory to the PATH variable you can just put the whole address on the console and it should work
/Applications/Postgres.app/Contents/MacOS/bin/psql -d template1 -U postgres

Related

Running psql from shell always ask for connection details

I am trying to run psql command from windows command line. However, command always ask for connection details even though connection details are given.
I have tried below two commands but none works:
"C:\Program Files\PostgreSQL\12\scripts\runpsql.bat -f d:\test.sql postgresql://postgres:password#localhost:5432/testdb
and
"C:\Program Files\PostgreSQL\12\scripts\runpsql.bat" -h localhost -d testdb -U postgres -p 5432 -f d:\test.sql
I have created password file to store password as mentioned here.
However, in command line, it asks for host name, database and other details.

Access Denied for User ODBC # localhost

i have installed Mysql on Windows7, i used mysql with mysql command line client & i also use Mysqldump in windows cmd, & it was working without any problem. But today, i tried to export database using mysqldump with this command in cmd
mysqldump –u root -p mypassword db_name > f:\mydb.sql
i tried many other commands and i always see error
Access Denied for User 'ODBC'#'localhost' (using password: yes) when trying to connect
as you can see, in mysqldump command i am using root as user then why i get user ODBC error ? one more thing, using mysql command line client i am still using mysql normally without any problem using root as user. i also tried to login in cmd with this command
mysql –u root -p mypassword
but still same error. and my password is 100% correct. kindly tell me how to solve this problem. Thanks
Have you tried storing pass in the cfg file? http://dev.mysql.com/doc/refman/5.7/en/password-security-user.html
try from command line:
mysql -u root -p
and then, when you are being asked for password, just type it.
Try the same with mysqldump command without typing your password after -p.

cannot connect to template1: 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 try to follow the OSM tutorial on psql and PostGis. But that seems rather hard... I am stuck here.
They suggest to create the user like:
sudo -u postgres createuser christoph
That did not work for me. I created the user like this:
$ sudo -u _postgres createuser -U postgres christoph
That worked without any problems. Then I tried this:
$ sudo -u _postgres createdb --encoding=UTF8 --owner=christoph gis
> cannot connect to template1: FATAL: role "_postgres" does not exist
How can _postgres not exist if I just created a role with it?!
I also do not understand why
$ createdb mydb
> -bash: createdb: command not found
does not work although
I added the path like:
$ export PSQL_DIR=/usr/local/pgsql/bin/
$ export PATH="$PSQL_DIR:$PATH"
before.
Executing the command like:
$ /usr/local/pgsql/bin/createdb mydb
> createdb: cannot create database: ERROR: permission denied to create database
I am looged in with the user who installed psql...
Is that connected?!
It is very easy to confuse system users (stored in /etc/passwd) with Postgres roles (stored inside the database cluster).
In your first command, you mention both types of user:
sudo -u _postgres means "use the system user _postgres to execute the following command"
the -U postgres in the command means "connect to the Postgres cluster with the role postgres"
However, in the next command:
sudo -u _postgres createdb --encoding=UTF8 --owner=christoph gis
You specify the same sudo -u _postgres, but are missing the -U argument to the actual Postgres command (you say you want christoph to own the new DB, but you don't say "I am christoph").
The Postgres command-line tools have a default behaviour of guessing that the Postgres role name is the same as the system user name. Since the system user is named _postgres (with leading underscore) and the Postgres role is named postgres (no leading underscore), this assumption fails, and you can't connect.

PostgreSQL - Scripting installation from Source

I'm trying to install PostgreSQL from source and script it for automatic installation.
Installing dependances, downloading and compiling PostgreSQL works good. But there are 3 commands that I need to run as Postgres User
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
/usr/local/pgsql/bin/createdb test
I saw this link but it doesn't work in my script here is the output :
Success. You can now start the database server using:
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data/
or
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l logfile start
server starting
createdb: could not connect to database template1: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
admin#ip-172-31-27-106:~$ LOG: database system was shut down at 2015-03-27 10:09:54 UTC
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
And the script :
sudo su postgres <<-'EOF'
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ start
/usr/local/pgsql/bin/createdb pumgrana
EOF
After that, I need to press enter and the server is running. My database is not created. It seems like the script tries to create the database then run the server but I'm not sure. Can someone help me?
There are a few things wrong with that script:
pg_ctl should get a -w argument, making sure it waits until PostgreSQL has started before exiting.
You don't have any error checking, so it'll just keep going if something doesn't work. At minimum you should use set -e at the start.
I also suggest using sudo rather than su, which is kind of obsolete these days. You never need sudo su, that's what sudo -u is for. Using sudo also makes it easier to pass environment variables in. So I'd write something like (untested):
sudo -u postgres PATH="/usr/local/pgsql/bin:$PATH" <<-'EOF'
set -e
initdb -D /usr/local/pgsql/data/
pg_ctl -D /usr/local/pgsql/data/ -w start
createdb pumgrana
EOF
You might want to pass PGPORT or some other relevant env vars into the script too.
Completely separately to this ... why? Why do this? If you're automating an install from source, why not just build a .deb or .rpm automatically instead, then install that?

Postgres user does not exist?

I have just installed Postgres and have been tinkering with it and various configurations for 1-2 hours.
I am stuck on being unable to change to the postgres-user
$ su - postgres yields the following error: su: unknown login: postgres
$ sudo -u postgres psql yields the following error: sudo: unknown user: postgres
These attempts are made as the normal user. Trying them as root has the exact same results. I have installed postgres via Homebrew on OS X, and I have read the instructions multple times.
psql: Logs me in with my default username
psql -U postgres: Logs me in as the postgres user
Sudo doesn't seem to be required for me.
I use Postgres.app for my OS X postgres database. It removed the headache of making sure the installation was working and the database server was launched properly. Check it out here: http://postgresapp.com
Edit: Credit to #Erwin Brandstetter for correcting my use of the arguments.
By psql --help, when you didn't set options for database name (without -d option) it would be your username, if you didn't do -U, the database username would be your username too, etc.
But by initdb (to create the first database) command it doesn't have your username as any database name. It has a database named postgres. The first database is always created by the initdb command when the data storage area is initialized. This database is called postgres.
So if you don't have another database named your username, you need to do psql -d postgres for psql command to work. And it seems it gives -d option by default, psql postgres also works.
If you have created another database names the same to your username, (it should be done with createdb) then you may command psql only. And it seems the first database user name sets as your machine username by brew.
psql -d <first database name> -U <first database user name>
or,
psql -d postgres -U <your machine username>
psql -d postgres
would work by default.
OS X tends to prefix the system account names with "_"; you don't say what version of OS X you're using, but at least in 10.8 and 10.9 the _postgres user exists in a default install. Note that you won't be able to su to this account (except as root), since it doesn't have a password. sudo -u _postgres, on the other hand, should work fine.
For me This was the solution on macOS
ReInstall the psql
brew install postgres
Start PostgreSQL server
pg_ctl -D /usr/local/var/postgres start
Initialize DB
initdb /usr/local/var/postgres
If this command throws an error the rm the old database file and re-run the above command
rm -r /usr/local/var/postgres
Create a new database
createdb postgres_test
psql -W postegres_test
You will be logged into this db and can create a user in here to login
psql -U postgres
Worked fine for me in case of db name: postgres & username: postgres. So you do not need to write sudo.
And in the case other db, you may try
psql -U yourdb postgres
As it is given in Postgres help:
psql [OPTION]... [DBNAME [USERNAME]]
I get exactly the same errors as kryshah with su - postgres and sudo -u postgres psql.
DanielM's answer gives also errors.
Outputs when wrong settings
Answer however from przbabu's comment.
masi$ psql
psql: FATAL: database "masi" does not exist
masi$ psql -U postgres
psql: FATAL: role "postgres" does not exist
masi$ psql postgres
psql (9.4.1)
Type "help" for help.
I think the some part of this problem may be in owner settings in OSX
masi$ ls -al /Users/
total 0
drwxr-xr-x 7 root admin 238 Jul 3 09:50 .
drwxr-xr-x 37 root wheel 1326 Jul 2 19:02 ..
-rw-r--r-- 1 root wheel 0 Sep 10 2014 .localized
drwxrwxrwt 7 root wheel 238 Apr 9 19:49 Shared
drwxr-xr-x 2 root admin 68 Jul 3 09:50 postgres
drwxr-xr-x+ 71 masi staff 2414 Jul 3 09:50 masi
but doing sudo chown -R postgres:staff /Users/postgres gives chown: invalid user: ‘postgres:staff’.
In short, this is not the solution the problem.
Use the tools provided by the postgres installation to create a user and database.
To get right settings and outputs
There are specific commands after postgres installation to add a new user to the database system.
After initdb, run the following as described here
createuser --pwprompt postgres
createdb -Opostgres -Eutf8 masi_development
psql -U postgres -W masi_development
To avoid the password request all the time, you have three choices as described here.
the discussion and answer here was massively helpful to me:
psql: FATAL: database "<user>" does not exist
You can try this to create a new role, you can name :
sudo adduser <new_user_role>
The solution is simple:
log in as root
and after:
su - postgres
psql

Resources