connecting to postgresql instance vs a specific database via lua? - postgresql

I'm not sure if used the right terminology in my question but here's what I'm trying to do. From the command line, I am used to running the following command :
psql -U postgres
and then I can see a list of all my databases by doing:
postgres=# \l
I'm wondering how to do the same thing programmatically in lua?
The following function is what I currently use to connect to a specific db:
local db_env, db_con
local connect_db = function()
if not con then
db_env = assert (luasql.postgres())
db_con = assert (db_env:connect(databasename, databaseUser, databasepassword))
end
end
Just wondering how I would change it to connect to the instance of postgresql server to see all the dbs that are hosted by my server.
Thanks.
Edit 1
Perhaps when I'm running the command
psql -U postgres
it is connecting to a default database?

In your code you have to connect to the database server and query the server for a list of databases. The database will return a recordset containing a list of the databases on that server.
A great walk-through of connecting to postgres and executing queries ( like the one above that hjpotter92 posted).
Hitting Postgres From Lua
Hope you find it helpful

Related

ERM from PostgreSQL via psql (SQL Shell)

I need to understand the relations between tables in a PostgreSQL database. I will not have the ability to download pgAdmin4 like I am used to working in. So after looking around I found pg_dump.exe built into PostgreSQL. I thought I could just do something like this in the SQL Shell (psql) to get a dump of the database and then have the ability to upload it into another system:
database=# pg_dump database > path/to/save/file.sql;
Based on the docs >> https://www.postgresql.org/docs/9.3/app-pgdump.html
But when I run that I get an error:
ERROR: syntax error at or near "pg_dump"
LINE 1: pg_dump database > path/to/save/file.sql;
^
I saw on this stack overflow question was similar to my issue with the pg_dump error. In the solutions, Adrian says that pg_dump does not work within psql. When I run the code that Adrian suggested, I also get an error.
Any thoughts on how I can use psql to get the information that I need of this database?
Note: I am accessing a Linux VM on a Windows machine.

Postgres: how to disconnect without logging out?

Using psql, how can I disconnect from an established connection without logging out?
To be more specific: Assuming the database server runs on localhost, I connect to the db server using
psql -U <user>
after that I am in the PSQL console. From there I can connect to one of the databases using
<user>=# \connect <database>
Now here is the question: How can I disconnect from this session without logging of from PSQL Console? I tried \disconnect but without any luck and using \q not only closes the session but kicks me out of PSQL entirely.
So is there a command which allows me to disconnect from one database and to reconnect to another database (using \connect <another_database>) without logging of from/ shutting down PSQL?
According to the POSTGRESQL docs at Postgres Manual, there is no meta command which will only close the current connection (without closing the PSQL application using \q). The only way to close the current connection is to connect to another database using \connect, as described in the docs mentioned above:
If the new connection is successfully made, the previous connection is closed.
Similiar information is also provided in the second answer to How to Disconnect from a database and go back to the default database in PostgreSQL? (thanks to #Barbara Laird for pointing this out)

ERROR: cannot execute CREATE TABLE in a read-only transaction

I'm trying to setup the pgexercises data in my local machine. When I run: psql -U <username> -f clubdata.sql -d postgres -x I get the error: psql:clubdata.sql:6: ERROR: cannot execute CREATE SCHEMA in a read-only transaction.
Why did it create a read-only database on my local machine? Can I change this?
Normally the most plausible reasons for this kind of error are :
trying create statements on a read-only replica (the entire instance is read-only).
<username> has default_transaction_read_only set to ON
the database has default_transaction_read_only set to ON
The script mentioned has in its first lines:
CREATE DATABASE exercises;
\c exercises
CREATE SCHEMA cd;
and you report that the error happens with CREATE SCHEMA at line 6, not before.
That means that the CREATE DATABASE does work, when run by <username>.
And it wouldn't work if any of the reasons above was directly applicable.
One possibility that would technically explain this would be that default_transaction_read_only would be ON in the postgresql.conf file, and set to OFF for the database postgres, the one that the invocation of psql connects to, through an ALTER DATABASE statement that supersedes the configuration file.
That would be why CREATE DATABASE works, but then as soon as it connects to a different database with \c, the default_transaction_read_only setting of the session would flip to ON.
But of course that would be a pretty weird and unusual configuration.
Reached out to pgexercises.com and they were able to help me.
I ran these commands(separately):
psql -U <username> -d postgres
begin;
set transaction read write;
alter database exercises set default_transaction_read_only = off;
commit;
\q
Then I dropped the database from the terminal dropdb exercises and ran script again psql -U <username> -f clubdata.sql -d postgres -x -q
I was having getting cannot execute CREATE TABLE in a read-only transaction, cannot execute DELETE TABLE in a read-only transaction and others.
They all followed a cannot execute INSERT in a read-only transaction. It was like the connection had switched itself over to read-only in the middle of my batch processing.
Turns out, I was running out of storage!
Write access was disabled when the database could no longer write anything. I am using Postgres on Azure. I don't know if the same effect would happen if I was on a dedicated server.
I had same issue for Postgre Update statement
SQL Error: 0, SQLState: 25006 ERROR: cannot execute UPDATE in a read-only transaction
Verified Database access by running below query and it will return either true or false
SELECT pg_is_in_recovery()
true -> Database has only Read Access
false -> Database has full Access
if returns true then check with DBA team for the full access and also try for ping in command prompt and ensure the connectivity.
ping <database hostname or dns>
Also verify if you have primary and standby node for the database
In my case I had a master and replication nodes, and the master node became replication node, which I believe switched it into hot_standby mode. So I was trying to write data into a node that was meant only for reading, therefore the "read-only" problem.
You can query the node in question with SELECT pg_is_in_recovery(), and if it returns True then it is "read-only", and I suppose you should switch to using whatever master node you have now.
I got this information from: https://serverfault.com/questions/630753/how-to-change-postgresql-database-from-read-only-to-writable.
So full credit and my thanks goes to Craig Ringer!
Dbeaver: In my case
This was on.
This doesn't quite answer the original question, but I received the same error and found this page, which ultimately led to a fix.
My issue was trying to run a function with temp tables being created and dropped. The function was created with SECURITY DEFINER privileges, and the user had access locally.
In a different environment, I received the cannot execute DROP TABLE in a read-only transaction error message. This environment was AWS Aurora, and by default, non-admin developers were given read-only privileges. Their server connections were thus set up to use the read-only node of Aurora (-ro- is in the connection url), which must put the connection in the read-only state. Running the same function with the same user against the write node worked.
Seems like a good use case for table variables like SQL Server has! Or, at least, AWS should modify their flow to allow temp tables to be created and dropped on read nodes.
This occurred when I was restoring a production database locally, the database is still doing online recovery from the WAL records.
A little bit unexpected as I assumed pgbackgrest was creating instantly recoverable restores, perhaps not.
91902 postgres 20 0 1445256 14804 13180 D 4.3 0.3 0:28.06 postgres: startup recovering 000000010000001E000000A5
If like me you are trying to create DB on heroku and are stuck as this message shows up on the dataclip tab
I did this,
Choose Resources from(Overview Resources Deploy Metrics Activity Access Settings)
Choose Settings out of (Overview, Durability, Settings, Dataclip)
Then in Administration->Database Credentials choose View Credentials...
then open terminal and fill that info here and enter
psql --host=***************.amazonaws.com --port=5432 --username=*********pubxl --password --dbname=*******lol
then it'll ask for password, copy-paste from there and you can run Postgres cmds.
I suddenly started facing this error on postgres installed on my windows machine, when I was running alter query from dbeaver, all I did was deleted the connection of postgres from dbeaver and created a new connection
If you are using Azure Database for PostgreSQL your server gets into read-only mode when the storage used is near total capacity.
The error you get is exactly:
ERROR: cannot execute XXXXXXXXX in a read-only transaction
https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/concepts-compute-storage
I just had this error. My cause was not granting permission to the SEQUENCE
GRANT ALL ON SEQUENCE word_mash_word_cube_template_description_reference_seq TO ronshome_user;
If you are facing this issue with an RDS instance cluster, please check your endpoint and use the Writer instance endpoint. Then it should work now.
Issue can be dur to Intellij config:
Go to Database view> click on Data Source Properties (Shift + enter)> (Select your data source)>
Options tab> Under Connection : uncheck Read-only
For me it was Azure PostgreSQL failing over to standby during maintaince in Azure and never failing back to master when PostgreSQL was in HA mode. You can check this event in Service Health and also check which zone you current VM is running from. If it's 2 and not 1 them most likely that's the result of events described above.

Password authentication fails when trying to connect to postgresql from clojure with jdbc

I'm learning web development in Clojure and I'm not at all an expert in either PostgreSQL or JDBC.
I'm going through the "Web development with Clojure" book and I'm stuck because I cannot connect to a PostgreSQL database, although I believe I have followed the tutorial step by step.
I connected to psql with
sudo -u postgres psql
Then I created an admin user with password 'admin'
postgres=# CREATE USER admin WITH PASSWORD 'admin';
CREATE ROLE
Then I created a database named reporting :
postgres=# CREATE DATABASE reporting OWNER admin;
CREATE DATABASE
No complaints so far.
Then from my clojure code, I attempt to connect to the database and create a table :
(ns reporting-example.models.db
(:require [clojure.java.jdbc :as sql]))
;defining the db connection
(def db
{:subprotocol "postgresql"
:subname "//localhost/reporting"
:user "admin"
:password "admin"})
;function for creating an 'employee' table
(defn create-employee-table []
(sql/create-table :employee
[:name "varchar(50)"]
[:occupation "varchar(50)"]
[:place "varchar(50)"]
[:country "varchar(50)"])
)
; then trying to actually create the table, here's the part that breaks with an exception :
(sql/with-connection db
(create-employee-table))
And I'm getting an ugly :
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "admin"
Which does not make sense to me, the credentials seem fine.
I tried the above code from both the Counterclockwise REPL and the Leiningen REPL. I'm on Ubuntu 13.10, if that matters.
Could someone explain to me what I am doing wrong here?
Thanks in advance!
Since you can't connect with psql using the same settings there are really three possibilities:
wrong/typo'd password
wrong/typo'd username
you are not connecting to the same server as you created the account on.
To check the latter, connect however you did to create the account and then run
show port;
Make sure it is the same as what you specify in your app. That isn't perfect - two pg instances can share a port if one isn't listening on TCP/IP and has a different socket directory - but it is a good first test.
You should also examine the PostgreSQL server error logs, as they may contain more detailed information about the nature of the authentication failure.
If you have not tried this already, review your pg_hba.conf file. It will be named something like
/etc/postgresql/9.6/main/pg_hba.conf (Ubuntu 16.04)
/var/lib/pgsql/9.3/data/pg_hba.conf (Fedora 20);
You may have to use find / -name pg_hba.conf to locate it.
At the bottom of the file, change the METHOD values to trust for local testing (see postgres docs for full information). Restart postgres to ensure everything is started clean and the new params are read:
> sudo systemctl restart postgresql # ubuntu
Hopefully this will cure your woes. It solved my problems on Ubuntu/Fedora.

"use database_name" command in PostgreSQL

I am beginner to PostgreSQL.
I want to connect to another database from the query editor of Postgres - like the USE command of MySQL or MS SQL Server.
I found \c databasename by searching the Internet, but its runs only on psql. When I try it from the PostgreSQL query editor I get a syntax error.
I have to change the database by pgscripting. Does anyone know how to do it?
When you get a connection to PostgreSQL it is always to a particular database. To access a different database, you must get a new connection.
Using \c in psql closes the old connection and acquires a new one, using the specified database and/or credentials. You get a whole new back-end process and everything.
You must specify the database to use on connect; if you want to use psql for your script, you can use "\c name_database"
user_name=# CREATE DATABASE testdatabase;
user_name=# \c testdatabase
At this point you might see the following output
You are now connected to database "testdatabase" as user "user_name".
testdatabase=#
Notice how the prompt changes. Cheers, have just been hustling looking for this too, too little information on postgreSQL compared to MySQL and the rest in my view.
In pgAdmin you can also use
SET search_path TO your_db_name;
The basic problem while migrating from MySQL I faced was, I thought of the term database to be same in PostgreSQL also, but it is not. So if we are going to switch the database from our application or pgAdmin, the result would not be as expected.
As in my case, we have separate schemas (Considering PostgreSQL terminology here.) for each customer and separate admin schema. So in application, I have to switch between schemas.
For this, we can use the SET search_path command. This does switch the current schema to the specified schema name for the current session.
example:
SET search_path = different_schema_name;
This changes the current_schema to the specified schema for the session. To change it permanently, we have to make changes in postgresql.conf file.
Use this commad when first connect to psql
=# psql <databaseName> <usernamePostgresql>
set search_path = 'schema name here'
while connecting to the postgres, you have to opt for default database to connect. If you have nothing, you can use 'postgres' as default.
You can use dbeaver to connect to postgres. UI is good
PgAdmin 4, GUI Tool: Switching between databases
In the PgAdmin Browser on the left hand side, right click on the database you are willing to switch to.
Select a QueryTool from the drop down menu (or any other option that you need, I will stick with the QueryTool for now).
You will see the QueryTool in the PgAdmin window, and on top you will see the active database and the role name.
Now you can write queries against the chosen database.
You can open multiple QueryTools for multiple database, and work with them as you do with your graphical text editor.
In order to be sure that you are querying the proper database, issue the following query:
SELECT session_user, current_database();