psql error when used with username containing at (#) and using URL - psql

I'm trying to connect to my PostgreSQL 11.3 server using CLI (psql). I have a requirement to set password in command line (cannot use environment variable PGPASSWORD).
I'm trying to use an URL to access database like this:
psql postgresql://username#server:password#server.full.name:5432/db_name
As you see, my login contains at (#) in it, e.g. my login name is: username#server.
I get error:
psql: Invalid port number: "password#server.full.name:5432/db_name"
I suppose that psql CLI parses first at (#) as a delimiter between username and server name. Is is a psql CLI bug?
How can I pass such username (containing #) in URL to start psql properly?
Thanks!
Update 1:
My colleague suggested to use %40 instead of colon : between login and password like this:
psql postgresql://username#server%40password#server.full.name:5432/db_name
But this has no effect (I use Windows 10, cmd.exe)

After some investigation, I found a solution (for the benefit of others):
psql postgresql://username%40server:password#server.full.name:5432/db_name
If you start it from cmd or bat file, mask percent sign (%) like this:
psql postgresql://username%%40server:password#server.full.name:5432/db_name

Related

How to call "mysql" with STDIN redirection from inside Rails

I am trying to use redirection to load a SQL database schema using:
system("mysql -p -h db.server.local -u admin -D some_db < schema.sql")
I expect it to prompt for a password, and it works correctly when run from IRB. However, when run via Rails' runner it fails
as if I had hit the Enter key at the password prompt. In Rails console it looks like this:
pry(main)> system("mysql -p -h db.server.local -u admin -D some_db < schema.sql")
Enter password: ERROR 1045 (28000): Access denied for user 'admin'#'mylocalpc' (using password: NO)
It did not give me a chance to enter the password. When I removed the redirection (<), it correctly prompts for a password. So it appears that somehow, when run via Rails, the STDIN redirection is disrupting the password prompt. I tried with backticks and had the same issue.
I assume that the mysql executable must be using some magic so that the STDIN redirection does not disrupt the password prompt, but it appears to be broken when the executable is launched via Rails.
Is Rails overriding System() and if so, is there some way to call the real System()?
Does anyone have other ideas of how to work around this, or what could be happening here?
The mysql executable is skipping the request for a password because in order to except stdin for the password, the command needs to be executed with TTY enabled. Ruby system command and backticks do not execute the command using a TTY enabled interface to the underlying system. There are a couple gems that allow for TTY enabled execution on the command line, I would check out the tty-command gem if you’d like to force the command prompt to ask for the password.
Though for the ideal solution that I would suggest, since you’re executing the command from within Rails, you should have access to Rails.credentials. I would store the necessary credentials (username and password) for the database within the credentials store, and use this to populate the -u and -P flag values for the mysql command. This will avoid the need to prompt for the password entirely.

Can psql commands be executed from Rails application

I want to know whether psql commands be executed from an Application ?
For example I want to make use of \crosstabview functionality which psql provides. It is a great feature to have when viewing the reports.
I have an application which uses Ruby on Rails. I'm thinking whether I can run the \crosstabview from the application.
These are features of psql application only. There is no way to use them via database driver, be it ActiveRecord or anything else. That's just a different thing.
However, you can have table view using, for example, table_view gem.
Can psql commands be executed from Rails application?
It can theoretically be done by running psql from ruby. Its just a really clunky solution.
The psql --command option only takes a single function name or a SQL string that is parsable by the database and which cannot use psql specific functions. Which means that you can run for example %x{ psql --command "\\h"} but not \crosstableview which needs input.
That leaves using PTY to open an interactive session.
# example using PTY to connect to an interactive psql shell
require 'pty'
require 'expect'
PTY.spawn('psql') do |output, input, pid|
output.expect /\=\#/ do
input.puts '\\conninfo'
output.each do |line|
puts line
end
end
end
While possible there is a much better solution - use the crosstab function from the tablefunc extension which can be used with ActiveRecord::Base.connection.execute.
I managed to do this with "\ir" option provided by psql. By this option, you can run the commands written in a file. So put all commands that you want to run in CLI in a single file.
Now you just have to connect to psql and pass filename to "\i" option.
Example: I created a file "a.rb" and it inlcudes the following-
psql -h localhost -U postgres
\ir filename

Connecting to a Progress Openedge database from ABL

This code works fine if I run it in the Progress Editor. If I save this as a .p file and click on right button "RUN", it gives me an error that database doesn't exist. I understand that maybe I should insert some code to connect to a database.
Does anybody know what statement I should use?
DEF STREAM st1.
OUTPUT STREAM st1 TO c:\temp\teste.csv.
FOR EACH bdName.table NO-LOCK:
PUT STREAM st1 UNFORMATTED bdName.Table.attr ";" SKIP.
END.
OUTPUT STREAM st1 CLOSE.
Exactly as you say you need to connect to your database. This can be done in a couple of different ways.
Connect by CONNECT statement
You can connect a database using the CONNECT statement. Basically:
CONNECT <database name> [options]
Here's a simple statement that is connecting to a local database named "database" running on port 43210.
CONNECT database.db -H localhost -S 43210.
-H specifies the host running the database. This can be a name or an IP-address. -S specifies the port (or service) that the database uses for connections. This can be a number or a service-name (in that case it must be specified in /etc/services or similar)
However you cannot connect to a database and work with it's tables in the same program. Instead you will need to connect in one program and then run the logic in a second program
/* runProgram.p */
CONNECT database -H dbserver -S 29000.
RUN program.p.
DISCONNECT database.
/* program.p */
FOR EACH exampletable NO-LOCK:
DISPLAY exampletable.
END.
Connect by command line parameters
You can simple add parameters in your startup command so that the new session connects to one or more databases from start.
Windows:
prowin32.exe -db mydatabase -H localhost -S 7777
Look at the option below (parameter file) before doing this
Connect by command line parameter (using a parameter file)
Another option is to use a parameter file, normally with the extension .pf.
Then you will have to modify how you start your session so instead of just doing prowin32.exe (if your on windows) you add the -pf parameter:
prowin32.exe -pf myparameterfile.pf
The parameterfile will then contain all your connection parameters:
# myparameterfile.pf
-db database -S localhost -P 12345
Hashtag (#) is used for comments in parameter files.
On Linux/Unix you would run:
pro -pf myparameterfile.pf
You can also mix the different ways for different databases used in the same session.

Getting error: Peer authentication failed for user "postgres", when trying to get pgsql working with rails

I'm getting the error:
FATAL: Peer authentication failed for user "postgres"
when I try to make postgres work with Rails.
Here's my pg_hba.conf, my database.yml, and a dump of the full trace.
I changed authentication to md5 in pg_hba and tried different things, but none seem to work.
I also tried creating a new user and database as per Rails 3.2, FATAL: Peer authentication failed for user (PG::Error)
But they don't show up on pgadmin or even when I run sudo -u postgres psql -l.
Any idea where I'm going wrong?
The problem is still your pg_hba.conf file*.
This line:
local all postgres peer
Should be:
local all postgres md5
After altering this file, don't forget to restart your PostgreSQL server. If you're on Linux, that would be sudo systemctl restart postgresql (on older systems: sudo service postgresql restart).
Locating hba.conf
Note that the location of this file isn't very consistent.
You can use locate pg_hba.conf or ask PostgreSQL SHOW hba_file; to discover the file location.
Usual locations are /etc/postgresql/[version]/main/pg_hba.conf and /var/lib/pgsql/data/pg_hba.conf.
These are brief descriptions of the peer vs md5 options according to the official PostgreSQL docs on authentication methods.
Peer authentication
The peer authentication method works by obtaining the client's
operating system user name from the kernel and using it as the allowed
database user name (with optional user name mapping). This method is
only supported on local connections.
Password authentication
The password-based authentication methods are md5 and password. These
methods operate similarly except for the way that the password is sent
across the connection, namely MD5-hashed and clear-text respectively.
If you are at all concerned about password "sniffing" attacks then md5
is preferred. Plain password should always be avoided if possible.
However, md5 cannot be used with the db_user_namespace feature. If the
connection is protected by SSL encryption then password can be used
safely (though SSL certificate authentication might be a better choice
if one is depending on using SSL).
After installing Postgresql I did the below steps.
Open the file pg_hba.conf. For Ubuntu, use for example /etc/postgresql/13/main$ sudo nano pg_hba.conf and change this line at the bottom of the file, it should be the first line of the settings:
local all postgres peer
to
local all postgres trust
Side note: If you want to be able to connect with other users as well, you also need to change:
local all all peer
to
local all all md5
If you used nano editor, exit with double Escape, x, y, Enter to save the config file.
Restart the server
$ sudo service postgresql restart
Output: * Restarting PostgreSQL 13 database server
Login into psql and set your password
$ psql -U postgres
db> ALTER USER postgres with password 'your-pass';
Output: ALTER ROLE
Side note: If you have other users, they will need a password as well:
db> ALTER USER my_user with password 'your-pass';
Then enter:
exit
Finally change the pg_hba.conf from
local all postgres trust
to
local all postgres md5
Restart the server again
$ sudo service postgresql restart
Output: * Restarting PostgreSQL 13 database server
Login at psql with postgres user
After restarting the postgresql server, the postgres user accepts the password that you chose:
psql -U postgres
Output:
Password for user postgres:
psql (13.4 (Ubuntu 13.4-1.pgdg20.04+1))
Type "help" for help.
And you are in psql:
postgres=#
Side note: Same now works for my_user if you added the user and password:
psql -d YOUR_DB_NAME -U my_user
Which will ask you for the new password of my_user.
Authentication methods details:
trust - anyone who can connect to the server is authorized to access the database
peer - use client's operating system user name as database user name to access it.
md5 - password-base authentication
for further reference check here
If you connect over localhost (127.0.0.1) you shouldn't experience that particular issue. I wouldn't muck much with the pg_hba.conf but instead I would adjust your connection string:
psql -U someuser -h 127.0.0.1 database
where someuser is your user you're connecting as and database is the database your user has permission to connect to.
Here is what I do on Debian to setup postgres:
http://www.postgresql.org/download/linux/debian/ (Wheezy 7.x)
as root …
root#www0:~# echo "deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main" >> /etc/apt/sources.list
root#www0:~# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
root#www0:~# apt-get update
root#www0:~# apt-get install postgresql-9.4
root#www0:~# su - postgres
postgres#www0:~$ createuser --interactive -P someuser
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n
postgres#www0:~$ createdb -O someuser database
postgres#www0:~$ psql -U someuser -h 127.0.0.1 database
Enjoy!
This has worked for me !!
sudo -u postgres psql
sudo psql --host=localhost --dbname=database-name --username=postgres
This solved my issue
If you have an issue, you need to locate your pg_hba.conf. The command is:
find / -name 'pg_hba.conf' 2>/dev/null
and after that change the configuration file:
Postgresql 9.3
Postgresql 9.4
The next step is: Restarting your db instance:
service postgresql-9.3 restart
If you have any problems, you need to set password again:
ALTER USER db_user with password 'db_password';
Go to this /etc/postgresql/9.x/main/ and open pg_hba.conf file
In my case:
$> sudo nano /etc/postgresql/9.3/main/pg_hba.conf
Replace peer with md5
So this will be changed to:
Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
This:
Database administrative login by Unix domain socket
local all postgres md5
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
Then restart pg server:
$> sudo service postgresql restart
Below is list of METHODS used to connect with postgres:
# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi",
# "krb5", "ident", "peer", "pam", "ldap", "radius" or "cert". Note that
# "password" sends passwords in clear text; "md5" is preferred since
# it sends encrypted passwords.
Note: If you have not create you postgres user yet. Create that and now you can access postgres server using that user credentials.
TIP: If it does not work after postgres restart then close terminal and open again.
Simplest solution without changing configs. (ubuntu)
Change user, then connect to database cli.
sudo -i -u postgres
psql
taken from https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-18-04
I was moving data directory on a cloned server and having troubles to login as postgres. Resetting postgres password like this worked for me.
root# su postgres
postgres$ psql -U postgres
psql (9.3.6)
Type "help" for help.
postgres=#\password
Enter new password:
Enter it again:
postgres=#
I had the same problem.
The solution from depa is absolutely correct.
Just make sure that u have a user configured to use PostgreSQL.
Check the file:
$ ls /etc/postgresql/9.1/main/pg_hba.conf -l
The permission of this file should be given to the user you have registered your psql with.
Further. If you are good till now..
Update as per #depa's instructions.
i.e.
$ sudo nano /etc/postgresql/9.1/main/pg_hba.conf
and then make changes.
If you want to keep the default config but want md5 authentication with socket connection for one specific user/db connection, add a "local" line BEFORE the "local all/all" line:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local dbname username md5 # <-- this line
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident
Most of the solutions are suggest editing the pg_hba.conf.
For you who don't want to edit the configuration file, you basically just need to log in to the postgres user. If you are using/in Linux server, use this command
sudo -i -u postgres
It'll create user postgres and then log in to it. Now try your psql command again.
You can also add postgres user a password with the command: (you should be in root user)
passwd postgres
This is works because according to this PostgreSQL's Documentation,
Peer Authentication
The peer authentication method works by obtaining the client's
operating system user name from the kernel and using it as the allowed
database user name (with optional user name mapping). This method is
only supported on local connections.
The edits above worked for me, after I figured out that I needed to restart the postgres server after making them.
For ubuntu:
sudo /etc/init.d/postgresql restart
Changing METHOD peer to trust in pg_hba.conf (/etc/postgresql/9.1/main/pg_hba.conf | line 85) solves the issue. Adding md5 asks for a password, hence if there is a requirement to avoid using passwords, use trust instead of md5.
Use host=localhost in connection.
PGconn *conn = PQconnectdb(
"host=localhost user=postgres dbname=postgres password=123"
);
Please follow the below steps
1). First, navigate to the /etc/postgresql/{your pg version}/main directory.
My version is 10 Then:
cd /etc/postgresql/10/main
2). Here resides the pg_hba.conf file needs to do some changes here you may need
sudo access for this.
sudo nano pg_hba.conf
3). Scroll down the file till you find this –
# Database administrative login by Unix domain socket
local all postgres peer
4). Here change the peer to md5 as follows.
# Database administrative login by Unix domain socket
local all all md5
peer means it will trust the authenticity of UNIX user hence does not
prompt for the password. md5 means it will always ask for a password,
and validate it after hashing with MD5.
5).Now save the file and restart the Postgres server.
sudo service postgresql restart
Now it should be ok.
This error may occur when you do not provide the host. The following scenario resembles it.
user#homepc:~$ psql -d test_db -U test_user
psql: error: FATAL: Peer authentication failed for user "test_user"
user#homepc:~$ psql -h localhost -d test_db -U test_user
Password for user test_user:
Providing host resolved my issue in psql command line. Try providing host in connection configuration for postgress in rails.
the below command works for me:
psql -d myDb -U username -W
You need just set METHOD to trust.
#TYPE DATABASE USER ADDRESS METHOD
local all all trust
And reload postgres server.
# service postgresql-9.5 reload
Changes in pg_hba.conf dont require RESTART postgres server. just RELOAD.
pg_config is for compliation information, to help extensions and client programs compile and link against PostgreSQL. It knows nothing about the active PostgreSQL instance(s) on the machine, only the binaries.
pg_hba.conf can appear in many other places depending on how Pg was installed. The standard location is pg_hba.conf within the data_directory of the database (which could be in /home, /var/lib/pgsql, /var/lib/postgresql/[version]/, /opt/postgres/, etc etc etc) but users and packagers can put it wherever they like. Unfortunately.
The only valid ways find pg_hba.conf is to ask a running PostgreSQL instance where it's pg_hba.conf is, or ask the sysadmin where it is. You can't even rely on asking where the datadir is and parsing postgresql.conf because an init script might passed a param like -c hba_file=/some/other/path when starting Pg.
What you want to do is ask PostgreSQL:
SHOW hba_file;
This command must be run on a superuser session, so for shell scripting you might write something like:
psql -t -P format=unaligned -c 'show hba_file';
and set the environment variables PGUSER, PGDATABASE, etc to ensure that the connection is right.
Yes, this is somewhat of a chicken-and-egg problem, in that if the user can't connect (say, after screwing up editing pg_hba.conf) you can't find pg_hba.conf in order to fix it.
Another option is to look at the ps command's output and see if the postmaster data directory argument -D is visible there, e.g.
ps aux | grep 'postgres *-D'
since pg_hba.conf will be inside the data directory (unless you're on Debian/Ubuntu or some derivative and using their packages).
If you're targeting specifically Ubuntu systems with PostgreSQL installed from Debian/Ubuntu packages it gets a little easier. You don't have to deal with hand-compiled-from-source Pg that someone's initdb'd a datadir for in their home dir, or an EnterpriseDB Pg install in /opt, etc. You can ask pg_wrapper, the Debian/Ubuntu multi-version Pg manager, where PostgreSQL is using the pg_lsclusters command from pg_wrapper.
If you can't connect (Pg isn't running, or you need to edit pg_hba.conf to connect) you'll have to search the system for pg_hba.conf files. On Mac and Linux something like sudo find / -type f -name pg_hba.conf will do. Then check the PG_VERSION file in the same directory to make sure it's the right PostgreSQL version if you have more than one. (If pg_hba.conf is in /etc/, ignore this, it's the parent directory name instead). If you have more than one data directory for the same PostgreSQL version you'll have to look at database size, check the command line of the running postgres from ps to see if it's data directory -D argument matches where you're editing, etc.
https://askubuntu.com/questions/256534/how-do-i-find-the-path-to-pg-hba-conf-from-the-shell/256711
Many of the other answers pertain to settings in the various config files, and the ones pertaining to the pg_hba.conf do apply and are 100% correct. However, make sure you are modifying the correct config files.
As others have mentioned the config file locations can be overridden with various settings inside the main config file, as well as supplying a path to the main config file on the command line with the -D option.
You can use the following command while in a psql session to show where your config files are being read (assuming you can launch psql). This is just a troubleshooting step that can help some people:
select * from pg_settings where setting~'pgsql';
You should also make sure that the home directory for your postgres user is where you expect it to be. I say this because it is quite easy to overlook this due to the fact that your prompt will display '~' instead of the actual path of your home directory, making it not so obvious. Many installations default the postgres user home directory to /var/lib/pgsql.
If it is not set to what it is supposed to be, stop the postgresql service and use the following command while logged in as root. Also make sure the postgres user is not logged into another session:
usermod -d /path/pgsql postgres
Finally make sure your PGDATA variable is set correctly by typing echo $PGDATA, which should output something similar to:
/path/pgsql/data
If it is not set, or shows something different from what you expect it to be, examine your startup or RC files such as .profile or .bash.rc - this will vary greatly depending on your OS and your shell. Once you have determined the correct startup script for your machine, you can insert the following:
export PGDATA=/path/pgsql/data
For my system, I placed this in /etc/profile.d/profile.local.sh so it was accessible for all users.
You should now be able to init the database as usual and all your psql path settings should be correct!
If you are facing this issue with rails and you know that you already have created that user-name with password along with correct rights then you just need to put following at the end of your database.yml file.
host: localhost
overall file will look like below
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: root
password: admin
host: localhost
You do not need to touch you pg_hba.conf file at all. Happy coding
My issue was that I did not type any server. I thought it is a default because of placeholder but when I typed localhost it did work.
If you are trying to locate this file in Cloud 9, you can do
sudo vim /var/lib/pgsql9/data/pg_hba.conf
Press I to edit/insert, press ESC 3 times and type :wq will save the file and quit
In my case, I was not even able to edit or see the content of pg_hba.conf file.
What worked was:
/etc/postgresql/14/main$ sudo vi pg_hba.conf
Vi editor with sudo permission.
On CentOS 7, PG 10, the file path is
/var/lib/pgsql/10/data/pg_hba.conf
Also, If you don't have access to the script from postgres account you can use the approach below.
$ cat ./init-user-db.sh | sudo -i -u postgres bash

Programmatical register a Postgres Server with pgAdmin using Delphi

Im working on an application using Postgres 9 and Delphi 7 as the front end,
For setting up the database connection to the application we have to do the following steps
1.Register a server in postgres
(i cannot post an image because im new user)
Name:=Localhost
Host:=Localhost
Port:=5432
SSL:=
MaintenanceDB:=myDB
Username:=Admin
password:=******
.
.
Service:=Localhost
2.Create the database and the tables.
Can any one tell me if the step 1.
can be done programatically?
Like for example
"C:\Program Files\PostgreSQL\9.0\bin\psql.exe" -h localhost -p 5432 -d myDB -U Admin Service=Localhost " ?
as of now its done manually by the user.
thanks in advance:)
The registered servers in pgAdmin are stored in the Windows registry under HKEY_CURRENT_USER\Software\pgAdmin III\Servers
The number of registered servers is stored in the Count attribute.
Then each server gets its own entry according to the number. So the first one is stored in HKEY_CURRENT_USER\Software\pgAdmin III\Servers\1, the second one in HKEY_CURRENT_USER\Software\pgAdmin III\Servers\2 and so on.
Have a look into your registry and create an approriate entry from within your Delphi application.

Resources