Problems starting Postgresql with sudo - postgresql

Running Ubuntu 17.10 and I am having an issue running Postgres. It was working a couple weeks ago, but now I have this problem.
sudo su postgres psql
/usr/bin/psql: line 19: use: command not found
/usr/bin/psql: line 20: use: command not found
/usr/bin/psql: line 21: use: command not found
/usr/bin/psql: line 22: use: command not found
/usr/bin/psql: psql: line 24: syntax error near unexpected token `$version,'
/usr/bin/psql: psql: line 24: `my ($version, $cluster);'
I have no idea what to do, the syntax looks normal.

Try
sudo -u postgres psql
instead.
I actually do not know what your command does. A quick look to man su does not really clarify what su will do with extra arguments but obviously it is not starting psql, it is probably feeding the file contents to the shell.

Related

setting up multiple postgresql 10 instances in same server

My company has two teams of devs, so I'm supposed to set up two postgresql instances on one machine on two different ports. We had a similar set-up on a test postgres VM but with versions 9.5/9.6. I installed postgres 10 without incident, and am trying to follow the instructions on the postgres10 docs here but when I try to user initdb or pg_ctl I get the following:
postgres#DevPostgres:/usr/local$ whereis pg_ctl
pg_ctl: /usr/share/man/man1/pg_ctl.1.gz
postgres#DevPostgres:/usr/local$ whereis initdb
initdb: /usr/share/man/man1/initdb.1.gz
postgres#DevPostgres:/usr/local$ pg_ctl -D /usr/local/pgsql/data initdb
pg_ctl: command not found
I thought these two programs would have been installed in the first installation, but when I follow the chain of symbolic links to a .gz file and unzip it and try to use it I get the following:
root#DevPostgres:~# /usr/share/postgresql/10/man/man1/initdb.1
/var/lib/postgresql/10-5433/
/usr/share/postgresql/10/man/man1/initdb.1: line 19: \" t
.\" Title: initdb
.\" Author: The PostgreSQL Global Development Group
.\" Generator: DocBook XSL Stylesheets v1.79.1 <http://docbook.sf.net/>
.\" Date: 2018
.\" Manual: PostgreSQL 10.3 Documentation
.\" Source: PostgreSQL 10.3
.\" Language: English
.\"
.TH "INITDB" "1" "2018" "PostgreSQL 10.3" "PostgreSQL 10.3 Documentation"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el .ds Aq : No such file or directory
/usr/share/postgresql/10/man/man1/initdb.1: line 20: .": command not found
/usr/share/postgresql/10/man/man1/initdb.1: line 21: .": command not found
/usr/share/postgresql/10/man/man1/initdb.1: line 22: .": command not found
/usr/share/postgresql/10/man/man1/initdb.1: line 23: .": command not found
/usr/share/postgresql/10/man/man1/initdb.1: line 24: .nh: command not found
/usr/share/postgresql/10/man/man1/initdb.1: line 25: syntax error near unexpected token `('
/usr/share/postgresql/10/man/man1/initdb.1: line 25: `.\" disable justification (adjust text to left margin only)'
Any advice would be much appreciated!

How to add Postgres extensions via .sh file

I'm trying to add extensions for Postgres by writing the following script (setup.sh):
sudo -u postgres psql my_database
CREATE EXTENSION adminpack;
when I do vagrant up, it supposed to run and add extensions automatically by running the script. However, I got the error message that
==> default: ERROR: syntax error at or near "exit"
==> default: LINE 1: exit
==> default: ^
==> default: /tmp/vagrant-shell: line 24: CREATE: command not found
Please note that I have installed all the necessary postgres stuff to run the code above. In addition, when I enter these command manually, it successfully creates the extension. Thanks to whoever that helps.
try:
sudo -u postgres psql my_database -c "CREATE EXTENSION adminpack"
https://www.postgresql.org/docs/current/static/app-psql.html
-c command
--command=command
Specifies that psql is to execute the given command string, command.
also consider using -f sql_file.sql for more complicated scripts, or smth like:
psql <<EOF
\x
SELECT NOW();
SELECT * FROM foo;
EOF

What does Usage: psql [wait] mean?

I'm trying to use psql version 9.6 on Mac OSX 10.11 and installed from the EnterpriseDB installer but I'm getting the following error:
MacBook-Pro:local me$ which psql
/usr/local/bin/psql
MacBook-Pro:local me$ /usr/local/bin/psql -h localhost; echo "Error code: $?"
Usage: /usr/local/bin/psql [wait]
Error code: 127
As you can see, I'm simply trying to connect to localhost and I'm getting a usage error despite following the syntax from the manual.
What is happening here?
Your bash is returning the error code 127: command not found.
See 127 Return code from $? for more details about this bash code.
Make sure that psql is in the directory you're trying to access.
Simple mistake - /usr/local/bin/psql was a symlink for /Library/PostgreSQL/9.6/scripts/runpsql.sh which doesn't accept any arguments... it's basically EnterpriseDB's shell script wrapper around the actual actual binary which is /Library/PostgreSQL/9.6/bin/psql.
I just needed to point psql to the real executable and not that shell script.

psql extra command line argument

Can someone tell me why I get the error extra command line argument here? When I use -f and give it the full path to the sql file it works fine. I would like to use a relative path instead so I was trying to use the \ir command.
psql -c \c postgresql://docker:1234/nbt?ssl=true -U admin -v username='user73291' -v recipeId=2 -c \ir '../../../resources/sql/myfile.sql'
Error:
psql: warning: extra command-line argument "../../../resources/sql/myfile.sql" ignored
ERROR: syntax error at or near "ir"
LINE 1: ir
^
Thanks!
\i is a meta-command to be used in the psql command line, not in the shell command line. What is the problem with -f?

altering privilege in psql interactive prompt

I am new to the use of psql prompt. I ran sudo -u postgres (psql version 9.3.9)
Then I connected to my database and I ran
=> ALTER DEFAULT PRIVILEGES FOR USER some_user IN SCHEMA public GRANT SELECT ON TABLES TO other_user;
and then I got this error message
ERROR: syntax error at or near "ALTER DEFAULT PRIVILEGES FOR USER"
LINE 1: ALTER DEFAULT PRIVILEGES FOR USER ckan_default IN SCHEMA pub..
How can I deal with that ?