1

https://www.postgresql.org/docs/current/libpq-example.html
compile the 1st example source code.

gcc test.c -L/usr/local/pgsql15/lib -I/usr/local/pgsql15/include -lpq

Cannot figure out the connection string. All the following failed.

./a.out postgresql:///test15?host=/tmp&port=5415
./a.out postgresql://test15?host=/tmp:5415
./a.out postgresql://test15?host=/tmp&port='5415'
./a.out postgresql://test15?host=/tmp:'5415'
./a.out postgresql://test15?host=/tmp&port=/tmp
./a.out postgresql:///tmp:5415/test15?

The error is almost similar:

connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
        Is the server running locally and accepting connections on that socket?

[1]+  Exit 1                  ./a.out postgresql:///test15?host=/tmp

cat /usr/local/pgsql15/data_42091266/postgresql.conf | rg unix return

unix_socket_directories = '/tmp'        # comma-separated list of directories
#unix_socket_group = ''                 # (change requires restart)
unix_socket_permissions = 0777          # begin with 0 to use octal notation
4
  • Does /tmp/.s.PGSQL.5432 exist? Commented Sep 15, 2022 at 20:01
  • @jjanes it does not. because port is 5415 Commented Sep 16, 2022 at 3:30
  • The last line of the error message block shows the command that was executed, and it did not give a port. Probably because the & is special to the shell, and ended the command before it got to the port part. Escape the ampersand. Commented Sep 16, 2022 at 12:17
  • @jjanes You are right! Commented Sep 16, 2022 at 12:34

2 Answers 2

1

Thanks to jjanes
The following two way works.

./a.out postgresql:///test15?host=/tmp\&port=5415

./a.out "postgresql:///test15?host=/tmp&port=5415"

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.htmlhttps://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html

Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to preserve the literal meaning of the special characters in the next paragraph, prevent reserved words from being recognized as such, and prevent parameter expansion and command substitution within here-document processing (see Here-Document).

The application shall quote the following characters if they are to represent themselves:

| & ; < > ( ) $ ` \ " '

Sign up to request clarification or add additional context in comments.

Comments

0

In addition to the postgresql:// and socket:// schemes addressed by others, you can also use the form:

"host=localhost port=5432  user=user1 dbname=db1 password=user1pass"

It should be passed as a single argument to the rest of the psql command. Working example below.

The port is optional if it is the system default (5432). If the socket is not in the system's default path to the socket directory (on many systems, /var/run/postgres, then you can specify the path instead of localhost.

In .pgpass, the hostname should match, and in the pg_hba.conf file, you need a line such as:

local all all scram-sha-256

Working example:

read -s DBPASS   # input password without echo
connstring="host=localhost user=testuser dbname=mydb password=$DBPASS"
psql -Atc 'select 1' "$connstring"

NOTE: THE ABOVE EXAMPLE IS INSECURE ON MULTI-USER SYSTEMS. When you do it this way, the command arguments and the password are visible to anyone else on the system who can run the "ps" command or view the "/proc" filesystem (Linux). The same goes (usually) for setting environment variables and/or using containers. Instead, save the password in .pgpass (make sure its not world-readable) or use peer authentication with ident maps (pg_hba, pg_ident configs).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.