Is there a way to compile pl/psql functions from within Sublime Text 2?
3 Answers
Add New Build System file with this commands and save it.
{
"cmd": ["psql", "-d", "your-database name",
"-U", "postgres",
"-f", "$file"],
"word_wrap": "false"
}

- 20,585
- 22
- 95
- 108

- 11
- 4
Yes you can. In order for this answer to work your network user must have access to the database. The way to do it is to create a new build system in Sublime for postgresql. You can do this by clicking Tools>Build System>New Build System.... Then replace the default build text with:
{
"path": "C:/Program Files (x86)/pgAdmin III/1.20/",
"cmd": ["psql.exe", "-f", "$file", "postgresql://db-staging-1:5432/mydbname"],
"selector": "source.postgresql",
"shell": true
}
Path: This should be the location of your psql.exe executable. Note if this path is in your environment variables path this line is unnecessary.
CMD: This is what will be run from the command line. I've included my connection information here as well. You'll need to replace it with the server path and port number for your database. Note, if you are having trouble with getting your build to run, the easiest way to debug what it's actually trying to run is to add echo on the front of this line:
"cmd": ["echo", "psql.exe", "-f", "$file", "postgresql://db-staging-1:5432/mydbname"],
Now the output of your build will be exactly what it's trying to run on the command line. If what it outputs here doesn't work on your command line then you need to change it to something that will.
Selector: This set the default build for postgresql files.
Shell: Treats the command as a shell script.
Now you can choose your build to be postgresql under Tools>Build System. After that a simple Ctrl+B will compile pl/pgsql functions to your database! Note that regular SQL can be run against your Postgresql database now as well.
If you regularly interact with more than one database at once, see this article as a good reference for setting up connections to multiple databases: How to make build system for PostgreSQL
Other Sublime text build options can be found here: http://sublimetext.info/docs/en/reference/build_systems.html

- 1
- 1

- 1,261
- 2
- 21
- 38
I actually just released a plugin (called DB1) that allows you to do just that. You can dynamically connect to and execute queries and functions against a PostgreSQL or MySQL database (I'm in the process of adding more databases too). A cool part about it is it doesn't require you to install anything on your computer (other than Sublime Text).
All you have to do is install DB1 through Package Control, and then in a view you can run the command DB1: Connect
to connect to your database. You can then execute sql in that view through one of the DB1: Execute
commands.
You can also just open the PSQL function (if you have it saved in a file) and execute the whole file.
To see how it works you can check out the DB1 Website or the documentation. Let me know if you have any questions about it!

- 512
- 6
- 17
-
I installed DB1 and when I try to connect to my database it seems to be using my user name as the database name. I choose new connection>postgreSQL and then enter a connect string of: dgrosskopf@servername-1:5432/mydb It then gives me the error: "FATAL: database "dgrosskopf" does not exist" What am I doing incorrectly? – danjuggler Nov 02 '15 at 18:01
-
Hey Daniel! DB1 Actually doesn't support the ```/mydb``` syntax, although that's something I've thought of doing. If you just enter ```dgrosskopf@servername-1:5432``` DB1 will then prompt you with a list of databases to choose from. Also, if you're just using the default port, then you really only need to enter ```dgrosskopf@servername```. – alexggordon Nov 03 '15 at 15:41
-
I just tried both of those options and it still gives me the same "dgrosskopf" database doesn't exist error. – danjuggler Nov 03 '15 at 16:26
-
Could you try navigating to ```"Preferences" > "Package Settings" > "DB1" > "Settings - User"``` and checking to see if there are stored connection settings for dgrosskopf@servername-1? What you'd be looking for is a "database" key in your connection settings. I've tried reproducing the error you're describing and I can't at all. The only place you can set the database like that is in the settings, so I think that setting may have made it in there. Also, this is postgresql, correct? – alexggordon Nov 03 '15 at 18:35
-
I'm guessing that may have been successful from the lack of a response, but if you run into any other issues, drop me at support@sequoiastudios.io. – alexggordon Nov 04 '15 at 16:21