Linux command works on terminal but not in ipython notebook - postgresql

I am running pg_dump mydb > ~/Desktop/mydb.dump on my mac shell which works perfectly fine.
But when i use the same command on jupyter notebook with ! or %%bash, it doesn't work, saying that /bin/sh: pg_dump: command not found
I do not have any problem with running other commands on notebook.
What could be the issue?
Thanks in advance.
EDIT & ANSWER:
so, even though i am running jupyter notebook via terminal, interestingly the env variables are not the same it seems like.
i do not know why exactly but instead of pg_dump, if i write the whole path for pg_dump, which i added to ~/.bash_profile on terminal, it works on jupyter notebook as well. in my case it is
/Applications/Postgres.app/Contents/Versions/11/bin/pg_dump mydb > ~/Desktop/mydb.dump

You should change your $PATH variable when you use bash in ipython notebook
Like:
export PATH=$PATH:/usr/local/bin/pg_dump && pg_dump mydb > ~/Desktop/mydb.dump

Related

PSQL -f Parameter Hanging on Windows 8.1

I've been using PSQL 14 on my Windows 10 desktop with Git Bash for a while now without issue. Recently I've had to transition to a Windows 8.1 laptop, and I've come across a problem with running the filename parameter for PSQL. When attempting to run a SQL file with the line psql.exe -U <user> -f src/sql/test.sql the terminal hangs until I use Ctrl+C to exit the command. I can run psql -U <user> and then copy & paste the SQL file text into the terminal to get the results I want, but I don't get why this issue is happening in the first place.
I've checked my PATH environment variables and I do have both the /bin and /lib paths in there. I have also tested changing -f with the < operator, which didn't change anything. Running PSQL on Windows 8.1 isn't an issue, it's just this particular command.

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/

Running PostgreSQL commands from anywhere on the terminal

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

How can the terminal in Jupyter automatically run bash instead of sh

I love the terminal feature and works very well for our use case where I would like students to do some work directly from a terminal so they experience that environment. The shell that launches automatically is sh and does not pick up all of my bash defaults. I can type "bash" and everything works perfectly. How can I make "bash" the default?
Jupyter uses the environment variable $SHELL to decide which shell to launch. If you are running jupyter using init then this will be set to dash on Ubuntu systems. My solution is to export SHELL=/bin/bash in the script that launches jupyter.
I have tried the ultimate way of switching system-wide SHELL environment variable by adding the following line to the file /etc/environment:
SHELL=/bin/bash
This works on Ubuntu environment. Every now and then, the SHELL variable always points to /bin/bash instead of /bin/sh in Terminal after a reboot.
Also, setting up CRON job to launch jupyter notebook at system startup triggered the same issue on jupyter notebook's Terminal.
It turns out that I need to include variable setting and sourcing statements for Bash init file like ~/.bashrc in CRON job statement as follows via the command $ crontab -e :
#reboot source /home/USERNAME/.bashrc && \
export SHELL=/bin/bash && \
/SOMEWHERE/jupyter notebook --port=8888
In this way, I can log in the Ubuntu server via a remote web browser (http://server-ip-address:8888/) with opening jupyter notebook's Terminal default to Bash as same as local environment.
You can add this to your jupyter_notebook_config.py
c.NotebookApp.terminado_settings = {'shell_command': ['/bin/bash']}
With Jupyter running on Ubuntu 15.10, the Jupyter shell will default into /bin/sh which is a symlink to /bin/dash.
rm /bin/sh
ln -s /bin/bash /bin/sh
That fix got Jupyter terminal booting into bash for me.