Running PostgreSQL commands from anywhere on the terminal - postgresql

First off, let me say that I'm new to both using Mac and PostgreSQL. I just installed Postgres using their installer and it was installed in /Library/Postgres/... when I tried running createdb from the terminal it returned an error createdb: command not found. I ended up using /library/postgresql/9.6/bin/createdb before I coud get it to work.
Here's my question, how do I set it so that I don't have to type in the full path again to use the createdb command.
I'd love a detailed explanation.
Thanks

First you need to execute the psql command to get into the postgresql interacive shell.
In your terminal:
psql
Postgresql interactive shell should start. In this shell
> createdb yourdatabasename;
Btw: If psql is not found you will probably need to add it to your path and restart your terminal, something like this with the path matching your machine:
export PATH=/Library/PostgreSQL/9.5/bin:$PATH

Related

Using the inline '--command' argument with psql doesn't produce logs in .psql_history

When I log into my PostgreSQL server manually on Ubuntu and execute a command, I can then find it logged in /root/.psql_history.
However when I try to run a command in a bash script via psql -c "*query goes here*", the command returns data but is not logged in .psql_history.
Has anyone encountered this before?
Command line retrieval and editing, as well as the history file, are functions of the “readline” library that is linked to psql.
Readline support is only active in interactive sessions, so there is also no history written if you invoke psql with the -c or -f options.

brew services start postgresql not working

I have installed postgres in Mac using command
brew install postgresql#9.6
and is successfully installed
brew services start postgresql#9.6 is successful also
==> Successfully started postgresql#9.6 (label: homebrew.mxcl.postgresql#9.6)
But when I tried to access Postgres from my Mac terminal psql postgresql#9.6 it prompt me weird error
-bash: /usr/local/bin/psql: No such file or directory
Am I doing something wrong, how to open psql from my terminal
Why there is no psql file in my /usr/local/bin
The accepted answer is incorrect.
If you install postgresql#9.6 via Homebrew, the right path for the /bin folder is:
/usr/local/Cellar/postgresql#9.6/9.6.15/bin
To connect, just type: psql postgres
If you're curious about the connections details, just type \conninfo and check it.
You must to enabling Postgres command line tools.
If you are using the default terminal, you are going to want to modify the file at
~/.bash_profile
. If you are using something like Oh My Zsh you are going to want to modify the file ~/.zshrc.
To edit this file you likely need to open it via the terminal, so open your terminal and type open ~/.bash_profile. You can replace the word open with subl or whatever text editor you prefer.
Once your zbash_profile or .zshrc file is open, add the following line to the end of the file:
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
After that you will need to quit and restart your terminal This is to make sure it reloads with the changes you just made.
Once you have restarted your terminal, try running psql.
psql -U postgres
You should get the following output.
psql (9.6.0)
Type "help" for help.
postgres=#
You can read more on https://www.calhoun.io/how-to-install-postgresql-9-6-on-mac-os-x/

opt/local/lib/postgresql94/bin/psql: cannot execute binary file

I installed postgresql94 and the server via macports and when I try to ‘su postgres psql’ getting following error...
opt/local/lib/postgresql94/bin/psql: cannot execute binary file
Why is this? What is wrong..? I type my password correctly and then that error appears...
I found the solution from a user on #macports on irc.freenode.net
You need to run psql from a shell after running:
su -l postgres
To get a new shell running as the user postgres :)
And then it works!

How to use/open psql?

I have installed On windows 7 postgreSQL 9.2 version.
Now, I need use psql, so where is this terminal?
Can you tell me please how to use for example this comand: psql databasename ?
Where must be type this?
Yes, may be this is dumb question, but...
You can follow this instruction
Open the command prompt
cd C:\postgresql-9.3.0-1-windows-x64-binaries\pgsql\bin (installed directory)
Run: initdb -U postgres -A password -E utf8 -W -D POSTGRESQL_ROOT\data
give super user password (Remember that)
you wiil get the success message
Success. You can now start the database server using:
"postgres" -D "POSTGRESQL_ROOT\data"
or
"pg_ctl" -D "POSTGRESQL_ROOT\data" -l logfile start
then, you are good to start the server
To stop the server : simply ctrl + c
You can use pgAdmin tool(http://www.postgresql.org/ftp/pgadmin3/release/v1.8.4/win32/) somewhat similar like SQL Mgt Studio
Reference : http://www.petrikainulainen.net/programming/tips-and-tricks/installing-postgresql-9-1-to-windows-7-from-the-binary-zip-distribution/
The easy way to start is
Open Run Window by Winkey + R
Type services.msc
Search Postgres service based on version installed.
Click stop, start or restart the service option.

Unable to restore the postgresql data through command prompt

I am trying to restore the postgres sql data from a file . I am trying to do so but it is not importing .
Here is the command which i am using:
postgres-# psql -hlocalhost -p5432 -u postgres -d test -f C:/wamp/www/test/database_backups/backup004.sql
Please help me what I am doing wrong .
I am using windows and the above command does not throws any error but it does not import data.
Regards
Surjan
The only immediate thing I can see there is the capitilsation of -u for username (should be -U).
Correction: You're typing the command line into the psql shell.
You should exit to the CMD.EXE shell, and try the command there. With the correct capitalisation of -U, by the way.
OR, use this to replay the script into that psql shell:
\i C:/wamp/www/test/database_backups/backup004.sql
The forward slashes don't cause a problem on my Windows machine.