3

As a developer, I often ssh right into my local database, just to test my application before pushing my code.

However, I find that every time I want to access Postgres, I have to type in

postgres@ubuntu:~$ /usr/local/pgsql/bin/psql test

whereas on my work machine, all I have to do is type

postgres@ubuntu:~$ psql --dbname=test --username=user

I tried creating a symlink, which was successful, but whenever I try connecting to it through this shortcut, I get the following error message:

psql: 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"?

How do I get this to work? In case it makes any difference, I'm using a self-compiled version of the 9.1.x series.

EDIT: I now know that this isn't really symlinking, but I'm not sure what it's called, or I'm sure I would have found it on Google already. Basically, I want it to run from the shell by typing in psql [OPTIONS] like rvm [OPTIONS] or ruby [OPTIONS].

I know there's a package named postgresql-client-common & a bunch of other Postgres packages through apt-get, but I would prefer not using the apt-get version so I can update Postgres on the fly.

3 Answers 3

5
+25

If it's just a matter of calling on psql directly you need to make sure the path "/usr/local/pgsql/bin" is included in $PATH when you are logging in. Oftentimes this has to do with setting the PATH variable appropriately in in /etc/profile (or ~/.profile) or if you are using bash, then this would be /etc/bashrc or ~/.bashrc.

e.g.

export PATH="$PATH:/usr/local/psql/bin"

Additionally.. the psql command expects the args as follow; psql <dbname> <username> (you can use --flags to specify the same).

If you supply "psql test"- then it will assume the username of the current user (as seen by whoami command).

Hope this helps.

3

Why not use the Linux Alias?

alias psql=/usr/local/pgsql/bin/psql

To make a permanent alias, just put this line in

~/.bashrc or ~/.bash_profile
0

What about the convenience of a function?

postgres@ubuntu:~$ function psqltest {
> /usr/local/pgsql/bin/psql test
> }
postgres@ubuntu:~$ 

Once defined, this will run /usr/local/pgsql/bin/psql test in your bash shell whenever you type

postgres@ubuntu:~$ psqltest

One last tip: Store the functions that you want to retain and use again in the $HOME/.bashrc file.

If you are interested in writing bash functions, I've found a tutorial https://ryanstutorials.net/bash-scripting-tutorial/bash-functions.php

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .